pandas :从每一行获取字符串的第二个字符 [英] Pandas: get second character of the string, from every row

查看:90
本文介绍了 pandas :从每一行获取字符串的第二个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Pandas中有一个数据数组,我正在尝试打印col1中每个字符串的第二个字符.我不知道该怎么做.我可以轻松地分别打印每个字符串的第二个字符,例如:

array.col1[0][1]

但是我想从每一行打印第二个字符,所以会有第二个字符的列表".

我尝试过

array.col1[0:][1]

但这只是返回第二行作为整个col1.

有什么建议吗?

解决方案

您可以使用

I've tried

array.col1[0:][1]

but that just returns the second line as a whole of col1.

Any advice?

解决方案

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.).

这篇关于 pandas :从每一行获取字符串的第二个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆