pandas :从每一行获取字符串的第二个字符 [英] Pandas: get second character of the string, from every row
问题描述
我在Pandas中有一个数据数组,我正在尝试打印col1中每个字符串的第二个字符.我不知道该怎么做.我可以轻松地分别打印每个字符串的第二个字符,例如:
array.col1[0][1]
但是我想从每一行打印第二个字符,所以会有第二个字符的列表".
我尝试过
array.col1[0:][1]
但这只是返回第二行作为整个col1.
有什么建议吗?
您可以使用
I've tried but that just returns the second line as a whole of col1. Any advice? You can use This 这篇关于 pandas :从每一行获取字符串的第二个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!array.col1[0:][1]
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
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.).