将不同聚合函数应用于数据帧的不同列的Python方法?为了高效地命名列? 我的问题
在SQL中,很容易将不同的聚合函数应用于不同的列,例如:将不同聚合函数应用于数据帧的不同列的Python方法?为了高效地命名列? 我的问题,python,pandas,dataframe,pandas-groupby,Python,Pandas,Dataframe,Pandas Groupby,在SQL中,很容易将不同的聚合函数应用于不同的列,例如: select item, sum(a) as [sum of a], avg(b) as [avg of b], min(c) as [min of c] 在熊猫身上,没有那么多。 提供的解决方案已被弃用: df.groupby('qtr').agg({"realgdp": {"mean_gdp": "mean", "std_gdp": "std&
select item, sum(a) as [sum of a], avg(b) as [avg of b], min(c) as [min of c]
在熊猫身上,没有那么多。
提供的解决方案已被弃用:
df.groupby('qtr').agg({"realgdp": {"mean_gdp": "mean", "std_gdp": "std"},
"unemp": {"mean_unemp": "mean"}})
我的解决方案
我设法找到的最差解决方案(主要基于我无法再找到的其他堆栈溢出问题)类似于底部的玩具示例,其中我:
- 定义一个包含我需要的所有计算的函数
- 分别计算每一列,然后将它们放在一个数据框中
- 将函数作为lambda函数应用:
我不是专家,但我通常使用这样的字典:
将熊猫作为pd导入
将numpy作为np导入
df=pd.DataFrame(列=['a','b','c','d',数据=np.random.rand(300,4))
df['city']=np.重复(['London','NewYork','Buenos Aires',100)
def func(x,df):
#在lambda函数中调用func();x是行,df是整个表
s_dict={}
s_dict['a'之和]=x['a'].sum()
s_dict['%of a']=x['a'].sum()/df['a'].sum(),如果df['a'].sum()=0.np.nan
s_dict['b'的平均值]=x['b'].平均值()
s_dict['a的加权平均值,由b'加权]=(x['a']*x['b']).sum()/x['b'].sum()如果x['b'].sum()大于0,则为np.nan
s_dict['c'之和]=x['c'].sum()
s_dict['d'之和]=x['d'].sum()
返回pd.系列(s_dict)
out=df.groupby('city')。应用(lambda x:func(x,df))
简洁性卓越-谢谢!我应该考虑一下:(
import pandas as pd
import numpy as np
df = pd.DataFrame(columns =['a','b','c','d'], data = np.random.rand(300,4))
df['city'] = np.repeat(['London','New York','Buenos Aires'], 100)
def func(x, df):
# func() gets called within a lambda function; x is the row, df is the entire table
b1 = x['a'].sum()
b2 = x['a'].sum() / df['a'].sum() if df['a'].sum() !=0 else np.nan
b3 = x['b'].mean()
b4 = ( x['a'] * x['b']).sum() / x['b'].sum() if x['b'].sum() >0 else np.nan
b5 = x['c'].sum()
b6 = x['d'].sum()
cols = ['sum of a',
'% of a',
'avg of b',
'weighted avg of a, weighted by b',
'sum of c',
'sum of d']
return pd.Series( [b1, b2, b3, b4, b5, b6] , index = cols )
out = df.groupby('city').apply(lambda x: func(x,df))