【发布时间】:2019-08-27 10:57:53
【问题描述】:
我有以下形式的数据框:
Price Quantity
Date Mat Str Type
2016-01-05 2016-02-19 3125.0 C 44 0.069
C 44 0.032
C 44 0.015
2016-01-06 2016-02-15 3169.0 P 69 0.069
我希望对 Quantity 列的行求和,但保持价格列不变(例如,用平均值制作另一个价格列)
我想过让 Price 列成为索引的一部分,但我的目标是比较 Price 和 Quantity Df 与其他类似形状的 Df 我可能会遇到一些问题。
这里有一些重新创建 df 的代码:
import pandas as pd
data = [{'Date':'2016-01-05', 'Mat':'2016-02-19', 'Str': 3125.0, 'Type': 'C', 'Quantity':0.069, 'Price':44},
{'Date':'2016-01-05', 'Mat':'2016-02-19', 'Str': 3125.0, 'Type': 'C', 'Quantity':0.032, 'Price':44},
{'Date':'2016-01-05', 'Mat':'2016-02-19', 'Str': 3125.0, 'Type': 'C', 'Quantity':0.015, 'Price':44},
{'Date':'2016-01-06', 'Mat':'2016-02-15', 'Str': 3169.0, 'Type': 'P', 'Quantity':0.069, 'Price':69}]
df1 = pd.DataFrame(data)
df1 = df1.set_index(['Date', 'Mat', 'Str', 'Type'])
感谢您的帮助!
【问题讨论】:
标签: python pandas dataframe multi-index