Pandas: get second character of the string, from every row

后端 未结 2 628
北海茫月
北海茫月 2021-01-19 00:26

I\'ve a array of data in Pandas and I\'m trying to print second character of every string in col1. I can\'t figure out how to do it. I can easily print the second character

2条回答
  •  一整个雨季
    2021-01-19 01:28

    You can use str to access the string methods for the column/Series and then slice the strings as normal:

    >>> df = pd.DataFrame(['foo', 'bar', 'baz'], columns=['col1'])
    >>> df
      col1
    0  foo
    1  bar
    2  baz
    
    >>> df.col1.str[1]
    0    o
    1    a
    2    a
    

    This str attribute also gives you access variety of very useful vectorised string methods, many of which are instantly recognisable from Python's own assortment of built-in string methods (split, replace, etc.).

提交回复
热议问题