简体   繁体   English

Pandas Dataframe 问题:应用函数添加带有结果的新列

[英]Pandas Dataframe Question: Apply function add new column with results

import pandas as pd

df = pd.DataFrame({'label': 'a a b c b c'.split(), 'Val': [2,2,6, 4,6, 8]})
df

  label  Val
0     a    2
1     a    2
2     b    6
3     c    4
4     b    6
5     c    8

df.groupby('label').apply(lambda x: x.mean())

 Val
label     
a      2.0
b      6.0
c      6.0

I'd like something like this.我想要这样的东西。 Where results are the values divided by the mean of the unique label:结果是值除以唯一标签的平均值:

label  Val  Results
0     a    2    1
1     a    2    1
2     b    6    1
3     c    4    0.6667
4     b    6    1
5     c    8    1.3333

Not entirely sure how to do it.不完全确定该怎么做。 Anyone have an idea?有人有想法吗? Tried this but didnt work:试过这个但没有用:

df['Results'] = df.groupby('label').apply(lambda x: x/x.mean())

You are close, add column Val after groupby for processing this column:您已关闭,在groupby之后添加列Val以处理此列:

df['Results'] = df.groupby('label')['Val'].apply(lambda x: x/x.mean())
print (df)
  label  Val   Results
0     a    2  1.000000
1     a    2  1.000000
2     b    6  1.000000
3     c    4  0.666667
4     b    6  1.000000
5     c    8  1.333333

Another idea for improve performance with GroupBy.transform for new Series filled by aggregated values and same size like original columns, so possible divide:使用GroupBy.transform为由聚合值填充的新Series提高性能的另一个想法,与原始列的大小相同,因此可能划分:

df['Results'] = df['Val'].div(df.groupby('label')['Val'].transform('mean'))

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

相关问题 如何在pandas groupby对象上应用函数并将结果保存回父数据帧的新列? - How to apply a function on a pandas groupby object and save the results back into a new column of the parent dataframe? 通过apply将静态列表添加到新的Pandas Dataframe列 - Add a static list to a new Pandas Dataframe column via apply 熊猫:将特定功能应用于列并在新数据框中创建列 - Pandas: apply a specific function to columns and create column in new dataframe 如何加快 Pandas 应用 function 在 dataframe 中创建新列? - How to speed up Pandas apply function to create a new column in the dataframe? 熊猫数据框应用功能基于选定的行创建新列 - Pandas dataframe apply function to create new column based on selected row 将函数应用于pandas数据框的列 - Apply a function to column of pandas dataframe python pandas 在 groupby 中应用 function,并将结果添加为数据框中的列 - python pandas apply function in groupby, and add results as column in data frame 熊猫数据框groupby + Apply +新列很慢 - Pandas dataframe groupby + apply + new column is slow 应用映射以在 pandas dataframe 中创建新列 - Apply mapping to create new column in pandas dataframe Pandas dataframe,如何按单列分组并将总和应用于多列并添加新的总和列? - Pandas dataframe, how can I group by single column and apply sum to multiple column and add new sum column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM