首先用Price表示多个列Number of Sales,然后使用^{}和聚合函数表示列名字典,然后用map和rename表示列中的多索引。地址:
df['Number of Sales'] *= df['Price']
d1 = {'Product':'size', 'Price':['sum', 'mean'], 'Number of Sales':'mean'}
df = df.groupby('Vendor').agg(d1)
df.columns = df.columns.map('_'.join)
d = {'Product_size':'No. of Product',
'Price_sum':'Sum of Prices',
'Price_mean':'Mean of Prices',
'Number of Sales_mean':'H Factor'
}
df = df.rename(columns=d).reset_index()
print (df)
Vendor No. of Product Sum of Prices Mean of Prices H Factor
0 A 4 121 30.25 6050.0
1 B 1 12 12.00 1440.0
2 C 2 47 23.50 587.5
3 H 1 45 45.00 9000.0
您可以使用pandaspivot_table来实现这一点。这是一个基于你的数据的例子。你知道吗
首先用} 和聚合函数表示列名字典,然后用
Price
表示多个列Number of Sales
,然后使用^{map
和rename
表示列中的多索引。地址:您可以使用groupby()执行此操作,如下所示:
那只是三列,但你可以算出其余的。你知道吗
相关问题 更多 >
编程相关推荐