Python的大 pandas :在阵列列中选择元素 [英] Python Pandas: selecting element in array column

查看:284
本文介绍了Python的大 pandas :在阵列列中选择元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的数据帧:

pa=pd.DataFrame({'a':np.array([[1.,4.],[2.],[3.,4.,5.]])})

我要选择列A,然后只对特定元素(即第一种:1。,2,3)

I want to select the column 'a' and then only a particular element (i.e. first: 1., 2., 3.)

什么我需要添加到:

pa.loc[:,['a']]

推荐答案

pa.loc [行] 选择带有标签的行

pa.loc [行,列] 选择它们是和<$ C的instersection细胞$ C>山坳

pa.loc [:, COL] 选择的所有的行和列名为山坳。注意,虽然这个作品它不是指一个数据帧的一列中的惯用的方式。对于你应该使用 PA〔'一']

pa.loc[:, col] selects all rows and the column named col. Note that although this works it is not the idiomatic way to refer to a column of a dataframe. For that you should use pa['a']

现在你必须列出你列的单元格,所以你可以使用的矢量字符串方法来访问这些列表的元素,像这样。

Now you have lists in the cells of your column so you can use the vectorized string methods to access the elements of those lists like so.

pa['a'].str[0] #first value in lists
pa['a'].str[-1] #last value in lists

这篇关于Python的大 pandas :在阵列列中选择元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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