如果某个日期的购买和出售数量相同,我想合并这些元素,而忽略其余元素,如下例所示。
数据集:
1/1/09 bought 100 AAPL
1/1/09 sold 100 AAPL
1/1/09 bought 35 AAPL
1/1/09 sold 35 AAPL
1/1/09 bought 105 AAPL
我希望结果数据框为:
1/1/09 bought 135 AAPL
1/1/09 sold 135 AAPL
1/1/09 bought 105 AAPL
答案 0 :(得分:0)
仅按组和求和:
my_var = ['bought','sold','bought','sold','bought']
dollar = [100,100,35,35,105]
date = ['2009-01-01','2009-01-01','2009-01-01','2009-01-01','2009-01-01']
sku = ['AAPL','AAPL','AAPL','AAPL','AAPL']
df = pd.DataFrame({'status':pd.Series(my_var),'dollars':pd.Series(dollar),'date':pd.Series(date),'SKU':pd.Series(sku)})
df.groupby(['SKU','status','date'],as_index = False)['dollars'].sum()