我有一个如下所示的数据框:import pandas as pdZ = pd.DataFrame({'Product': ['Apple', 'Apple', 'Apple', 'Orange', 'Orange], 'Selling Price': [1.1, 1.2, 1.3, 2.1, 2.2]})有数千种独特的产品和数亿的售价。我如何有效地报告每种独特产品的平均售价?Result = pd.DataFrame({'Product': ['Apple', 'Orange'], 'Average Selling Price': [1.2, 2.15]})挑战在于数据存储在数百个不同的 .csv 文件中(文件名存储在列表中files),我无法同时将其加载到我的环境中。所以我会做类似的事情for i in files: X = pd.read_csv(i) # add unique products to the data frame Z # add the sum of their selling prices to Z # add the number of times the product was sold# for each unique product, divide the sum of selling prices by the number of times that product was sold感谢您的任何帮助,您可以提供!
1 回答
![?](http://img1.sycdn.imooc.com/545868190001d52602200220-100-100.jpg)
当年话下
TA贡献1890条经验 获得超9个赞
final_df = pd.DataFrame()
for i in files:
X = pd.read_csv(i)
X_agg = X.groupby('Product', as_index=False).agg({'Selling Price':['count', 'sum']})
X_agg.columns = ['Product', 'sale_count', 'selling_sum']
final_df = pd.concat([final_df, X_agg])
final_df = final_df.groupby('Product', as_index=False).agg({'sale_count':'sum', 'selling_sum':'sum'})
添加回答
举报
0/150
提交
取消