我有以下代码:
import pandas as pd
df = pd.DataFrame(
{
'Fruits': ['Apple', 'Apple', 'Apple', 'Orange', 'Banana', 'Orange'],
'Price': [100, 300, 250, 300, 80, 150],
'Amount': [ 2, 5, 5, 10, 10, 8]
}
)
# Display DataFrame
print(df)
#df1 = df.groupby(['Fruits'], as_index=False).agg({'Price': 'sum', 'Amount': 'sum'})
#print(df1)
df1 = df.groupby(['Fruits'],as_index=False).sum()
print(df1)
def f(x):
return x[1]/x[2]
df1['Average'] = df1.apply(f, axis=1)
print(df1)
就像上面的例子一样,我可以在不调用函数的情况下为相同的"水果"创建一个额外的平均价格列吗?
数据:
>>> import pandas as pd
>>> df = pd.DataFrame(
>>> {
>>> 'Fruits': ['Apple', 'Apple', 'Apple', 'Orange', 'Banana', 'Orange'],
>>> 'Price': [100, 300, 250, 300, 80, 150],
>>> 'Amount': [ 2, 5, 5, 10, 10, 8]
>>> }
>>> )
>>> df1 = df.groupby(['Fruits'],as_index=False).sum()
>>> df1
Fruits Price Amount
0 Apple 650 12
1 Banana 80 10
2 Orange 450 18
如果你只需要一个变量,你可以简单地做df1['Average'] = df1.Price/df1.Amount
;如果你需要多个变量,你可以使用'assign'
>>> df1 = df1.assign(Average = lambda x:x.Price / x.Amount,
Average_2 = lambda x:x.Price / x.Amount
)
>>> df1
Fruits Price Amount Average Average_2
0 Apple 650 12 54.166667 54.166667
1 Banana 80 10 8.000000 8.000000
2 Orange 450 18 25.000000 25.000000