[英]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.