Python 基于group by之后的另一列创建新的数据框列
初始数据帧:Python 基于group by之后的另一列创建新的数据框列,python,pandas,pandas-groupby,Python,Pandas,Pandas Groupby,初始数据帧: import pandas as pd df = pd.DataFrame({'index': [0,0, 1,1, 2,2], 'Name': ['John', 'John', 'Mike', 'Mike', 'Tim', 'Tim'], 'Value': ['AAA', 'BBB', 'AAA', 'CCC', 'CCC', 'BBB'], 'Metric':[10, 20, 15,30, 35, 25],
import pandas as pd
df = pd.DataFrame({'index': [0,0, 1,1, 2,2], 'Name': ['John', 'John', 'Mike', 'Mike', 'Tim', 'Tim'],
'Value': ['AAA', 'BBB', 'AAA', 'CCC', 'CCC', 'BBB'], 'Metric':[10, 20, 15,30, 35, 25],
'Direction': ['BUY', 'SELL', 'BUY', 'SELL', 'BUY', 'SELL']})
index Name Value Metric Direction
0 John AAA 10 BUY
0 John BBB 20 SELL
1 Mike AAA 15 BUY
1 Mike CCC 30 SELL
2 Tim CCC 35 BUY
2 Tim BBB 25 SELL
- 新列“新值”
sell_value = df.loc[df.Direction == 'SELL', 'Value']
buy_value = df.loc[df.Direction == 'BUY', 'Value']
df.loc[df.Direction == 'BUY', "Value_New"] = sell_value
df.loc[df.Direction == 'SELL', "Value_New"] = buy_value
df['Metric_New'] = df.loc[df.Direction == "SELL", 'Metric']
结果:
index Name Value Metric Direction Value_New
0 John AAA 10 BUY BBB
0 John BBB 20 SELL AAA
1 Mike AAA 15 BUY CCC
1 Mike CCC 30 SELL AAA
2 Tim CCC 35 BUY BBB
2 Tim BBB 25 SELL CCC
index Name Value Metric Direction Metric_New
0 John AAA 10 BUY 20
0 John BBB 20 SELL 20
1 Mike AAA 15 BUY 30
1 Mike CCC 30 SELL 30
2 Tim CCC 35 BUY 25
2 Tim BBB 25 SELL 25
- 新列“公制_新”
sell_value = df.loc[df.Direction == 'SELL', 'Value']
buy_value = df.loc[df.Direction == 'BUY', 'Value']
df.loc[df.Direction == 'BUY', "Value_New"] = sell_value
df.loc[df.Direction == 'SELL', "Value_New"] = buy_value
df['Metric_New'] = df.loc[df.Direction == "SELL", 'Metric']
结果:
index Name Value Metric Direction Value_New
0 John AAA 10 BUY BBB
0 John BBB 20 SELL AAA
1 Mike AAA 15 BUY CCC
1 Mike CCC 30 SELL AAA
2 Tim CCC 35 BUY BBB
2 Tim BBB 25 SELL CCC
index Name Value Metric Direction Metric_New
0 John AAA 10 BUY 20
0 John BBB 20 SELL 20
1 Mike AAA 15 BUY 30
1 Mike CCC 30 SELL 30
2 Tim CCC 35 BUY 25
2 Tim BBB 25 SELL 25
谢谢你的帮助。到目前为止,我找到的最好的解决方案是通过索引进行for循环
for idx in df['index'].unique():
sell_value = df.loc[(df['index'] == idx) & (df.Direction == 'SELL'), 'Value']
buy_value = df.loc[(df['index'] == idx) & (df.Direction == 'BUY'), 'Value']
df.loc[(df['index'] == idx) & (df.Direction == 'SELL'), 'CPBook'] = buy_value.values
df.loc[(df['index'] == idx) & (df.Direction == 'BUY'), 'CPBook'] = sell_value.values
sell_metric = df.loc[(df['index'] == idx) & (df.Direction == "SELL"), 'Metric']
df.loc[(df['index'] == idx),'Metric_New'] = sell_metric.values