亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

計算 Pandas Dataframe 中每種產品的平均價格

計算 Pandas Dataframe 中每種產品的平均價格

陪伴而非守候 2023-08-08 15:36:12
我有一個如下所示的數據框: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 回答

?
當年話下

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'})


查看完整回答
反對 回復 2023-08-08
  • 1 回答
  • 0 關注
  • 202 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號