简体   繁体   English

在 pandas dataframe 的 dict 行中创建新的元素计数列

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

相关问题 计算字符串中的位数,然后在 Pandas 数据框中创建具有计数的新列 - Count number of digits in a string, then create new column with counts in Pandas dataframe 创建一个新列作为 Pandas DataFrame 的计数 - Create a new column as a count of the Pandas DataFrame 通过将每一行转换为 pandas dataframe 中的字典来创建新列 - Make new column by turning each row into a dict in a pandas dataframe 如何在groupby pandas dataFrame中创建具有条件计数的新列 - How to create a new column with a conditional count in a groupby pandas dataFrame 熊猫,如何计算分组数据框中的出现次数并创建新列? - Pandas, how to count the occurance within grouped dataframe and create new column? Pandas 根据来自另一个 dataframe 的计数和条件创建新列 - Pandas Create new column based on a count and a condition from another dataframe 新列中的 Pandas 行值以进行 dict - Pandas row value in a new column to dict 熊猫数据框应用功能基于选定的行创建新列 - Pandas dataframe apply function to create new column based on selected row 为每一行运行一个函数并创建一个新的 Column Pandas Dataframe - Run a function for each row and create a new Column Pandas Dataframe 根据上一行的值在熊猫数据框中创建一个新列 - Create a new column in a pandas dataframe based on values found on a previous row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM