[英]Create new column of count number of elem in row of dict of pandas dataframe
I have the following df:我有以下df:
values_list = [[15, {'num':[0]}, 100], [20, {'num':[0]}, 50], [25, {'num':[0]}, 80],
[45, {'num':[0], 'option':[1]}, 48], [40, {'num':[0]}, 70], [41, {'num':[0]}, 90],
[51, {'num':[0]}, 111]]
df = pd.DataFrame(values_list, columns=['Field_1', 'Field_2', 'Field_3'])
Fields_2 is a collomn of dict. Fields_2 是一个 dict 的 colomn。 I would like to perform the len following function and put it in a new column if I do:我想在 function 之后执行 len ,如果我这样做的话,把它放在一个新的列中:
len(df.Field_2[3])
output = 2 (and 1 for the other indexes) output = 2(其他索引为 1)
What I would like as result is a DF as follows我想要的结果是如下的DF
I tried the following lambda function but it doesn't seem to work as I get a column with the len of the column not the row我尝试了以下 lambda function 但它似乎不起作用,因为我得到的列是列的 len 而不是行
df = df.assign(elem=lambda x: (len(x['Field_2'])))
i would have expected something more like this but this gives an error我本来期望更像这样的东西,但这会产生错误
df = df.assign(elem=lambda x: (x[len(x['Field_2'])]))
Could someone point me out how to solve this issue?有人可以指出我如何解决这个问题吗?
Use len
per values in Series.apply
or Series.map
:对Series.apply
或Series.map
中的每个值使用len
:
df = df.assign(elem=lambda x: x['Field_2'].apply(len))
print (df)
Field_1 Field_2 Field_3 elem
0 15 {'num': [0]} 100 1
1 20 {'num': [0]} 50 1
2 25 {'num': [0]} 80 1
3 45 {'num': [0], 'option': [1]} 48 2
4 40 {'num': [0]} 70 1
5 41 {'num': [0]} 90 1
6 51 {'num': [0]} 111 1
Or solution from @Don'tAccept, thank you:或来自@Don'tAccept 的解决方案,谢谢:
df = df.assign(elem=df['Field_2'].apply(len))
Or solution from @Asish M., thank you:或@Asish M. 的解决方案,谢谢:
df = df.assign(elem=df['Field_2'].str.len())
The below should work:以下应该工作:
df["elem"] = df["Field_2"].apply(len)
You can use lambda when you convert each dictionary to list and take its length:当您将每个字典转换为列表并获取其长度时,您可以使用 lambda :
df['elem'] = df['Field_2'].apply(lambda x: len(list(x)))
Output: Output:
Field_1 Field_2 Field_3 elem
0 15 {'num': [0]} 100 1
1 20 {'num': [0]} 50 1
2 25 {'num': [0]} 80 1
3 45 {'num': [0], 'option': [1]} 48 2
4 40 {'num': [0]} 70 1
5 41 {'num': [0]} 90 1
6 51 {'num': [0]} 111 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.