如何在数据框的数组列中选择元素? [英] How do I select an element in array column of a data frame?

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

问题描述

我有以下数据框:

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[row]选择带有标签row的行.

pa.loc[row, col]选择rowcol

pa.loc[:, col]选择所有行和名为col的列.请注意,尽管这可行,但这并不是引用数据框的列的惯用方法.为此,您应该使用pa['a']

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

这篇关于如何在数据框的数组列中选择元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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