简体   繁体   中英

Create new data frame columns based on another column after group by

Initial Data Frame:

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
  • New Column "Value_New"

Grouped by "index", create new column "Valeu_New" equal to the counter-direction. When direction == "BUY", "Value_new" takes value of Direction == "SELL" and the other way around. For each group, it would be like:

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

Result:

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
  • New Column "Metric_New"

Grouped by "index", create new column "Metric_New" equal to Metric value of Direction == "SELL". Ignoring the Metric value of the BUY. For each group, it would be like:

df['Metric_New'] = df.loc[df.Direction == "SELL", 'Metric']

Result:

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

Thanks for the help.

The best solution I found so far is a for loop through indexes...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM