如何在数据框的数组列中选择元素?

编程入门 行业动态 更新时间:2024-10-28 04:22:46
本文介绍了如何在数据框的数组列中选择元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下数据框:

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]选择row和col

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

更多推荐

如何在数据框的数组列中选择元素?

本文发布于:2023-10-19 11:23:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1507347.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   元素   数据   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!