简体   繁体   English

查找数据框列中的元素数

[英]find the number of elements in a column of a dataframe

I want to file the row length of a column from the dataframe.我想从数据框中归档一列的行长。

dataframe name- df数据框名称- df

sample data:
a   b   c
1   d   ['as','the','is','are','we']
2   v   ['a','an']
3   t   ['we','will','pull','this','together','.']

expected result:
a   b   c                                          len
1   d   ['as','the','is','are','we']               5
2   v   ['a','an']                                 2
3   t   ['we','will','pull','this','together','.'] 6

Till now, i have just tried:到目前为止,我刚刚尝试过:

df.loc[:,'len']=len(df.c)

but this gives me the total rows present in the dataframe.但这给了我数据框中存在的总行数。 How can i get the elements in each row of a specific column of a dataframe?如何获取数据框特定列的每一行中的元素?

One way, is to use apply and calculate len一种方法是使用apply并计算 len

In [100]: dff
Out[100]:
   a  b                                    c
0  1  d               [as, the, is, are, we]
1  2  v                              [a, an]
2  3  t  [we, will, pull, this, together, .]

In [101]: dff['len'] = dff['c'].apply(len)

In [102]: dff
Out[102]:
   a  b                                    c  len
0  1  d               [as, the, is, are, we]    5
1  2  v                              [a, an]    2
2  3  t  [we, will, pull, this, together, .]    6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 pandas DataFrame 中查找 null 元素的数量 - How to find the number of null elements in a pandas DataFrame 在以字符串开头的数据框列中查找所有元素 - Find all elements in dataframe column that startswith string 如何在 dataframe 的列中查找元素的编号 - How to find the number of an element in a column of a dataframe 查找数据框值并返回列号 - Find Dataframe value and return column number 从另一个 DataFrame 中查找并计算一个 DataFrame 的元素数量 - Find and count the number of elements of one DataFrame from another DataFrame 如何知道在 Pandas 数据框中存储为数组的列中的元素数 - How to know the number of elements in a column that are stored as an array in Pandas dataframe 在熊猫数据框中创建一个包含组中数字元素的列(groupby) - Creating a column in a pandas dataframe containing the number elements in the group (groupby) 如何计算 Pandas dataframe 中列中唯一元素的数量 - How to count the number of unique elements in a column in a Pandas dataframe 使用可变数量的元素和前导文本扩展一列 pandas dataframe - Expanding a column of pandas dataframe with variable number of elements and leading texts 查找以列表形式存在的列元素的数据框索引的最快方法 - Fastest way to find dataframe indexes of column elements that exist as lists
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM