简体   繁体   English

分组后基于另一列创建新的数据框列

[英]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"新列“Value_New”

Grouped by "index", create new column "Valeu_New" equal to the counter-direction.按“索引”分组,创建与反方向相等的新列“Valeu_New”。 When direction == "BUY", "Value_new" takes value of Direction == "SELL" and the other way around.当 direction == "BUY" 时,"Value_new" 取 Direction == "SELL" 的值,反之亦然。 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"新列“Metric_New”

Grouped by "index", create new column "Metric_New" equal to Metric value of Direction == "SELL".按“索引”分组,创建新列“Metric_New”,等于 Direction ==“SELL”的度量值。 Ignoring the Metric value of the BUY.忽略 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 循环...

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据数据框中另一列的值创建新列 - Create new column based on a value of another column in a data-frame 基于现有列在 pandas 数据框中创建新列 - Create new columns in pandas data frame based on existing column 尝试使用 Pandas 数据框中其他两列的 groupby 基于另一列创建新的滚动平均列时出错 - Error when trying to create new rolling average column based on another column using groupby of two other columns in pandas data frame 根据另一个数据框对数据框的列进行分组 - Group a column of a data frame based on another dataframe 根据在另一列和公共列中找到的范围在数据框中创建一个新列 - Create a new column in data frame based on the range found in another column and a common column 比较两个熊猫数据框列的元素,并基于第三列创建一个新列 - Compare elements of two pandas data frame columns and create a new column based on a third column 基于现有数字列、字符串列表作为列名和元组列表作为值在数据框中创建新列 - Create new columns in a data frame based on an existing numeric column, a list of strings as column names and a list of tuples as values 根据另一个数据框创建新的数据框 - Create new data frame based on another data frame 如何根据是否存在另一组列来创建新列 - How to create new columns based on whether another group of Columns Exists 如何根据数据框中日期列之间的平均差创建新列? - How to create a new column based on the mean difference between date columns in a data frame?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM