简体   繁体   English

如何根据 Pandas DataFrame 中的条件添加每组具有重复值的新列?

[英]How do I add a new column with a repeated value per group based on condition in a Pandas DataFrame?

This is an example DataFrame.这是一个示例数据帧。

RootProduct | Product | Value
    A           A        1  
    A           B        2   
    A           C        3
    D           D        4
    D           E        5  

How can I add a fourth column, repeating the value present in the Value column when RootProduct == Product grouped by RootProduct ?RootProduct == ProductRootProduct分组时,如何添加第四列,重复Value列中的Value

This would result in the following DataFrame这将导致以下 DataFrame

RootProduct | Product | Value  | RootValue
    A           A        1          1
    A           B        2          1
    A           C        3          1 
    D           D        4          4 
    D           E        5          4

Idea is compare both columns by boolean indexing with Series.eq and then create Series by index with Product by DataFrame.set_index , so possible use Series.map by column RootProduct :想法是通过两列比较boolean indexingSeries.eq然后创建Series的指数与ProductDataFrame.set_index ,因此可能使用Series.mapRootProduct

s = df[df['RootProduct'].eq(df['Product'])].set_index('Product')['Value']
df['RootValue'] = df['RootProduct'].map(s)
print (df)
  RootProduct Product  Value  RootValue
0           A       A      1          1
1           A       B      2          1
2           A       C      3          1
3           D       D      4          4
4           D       E      5          4

Detail of Series : Series详情:

print (s)
Product
A    1
D    4
Name: Value, dtype: int64

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

相关问题 如何基于熊猫数据框中的行条件添加新列? - How to add new column based on row condition in pandas dataframe? 如何根据条件将级别添加到 pandas dataframe 中的新列? - how to add levels to a new column in pandas dataframe based on a condition? 如何为 Pandas 数据框列中的每个唯一值添加重复的月份行? - How do I add repeated month rows for every unique value in a pandas dataframe column? 如何在 pandas dataframe 中添加基于日期条件的值的列? - How to add a column with value based on date condition in pandas dataframe? 如何根据条件在 pandas dataframe 中添加新行? - How can I add a new line in pandas dataframe based in a condition? pandas 根据条件将值添加到新列 - pandas add value to new column based on condition 在 pandas dataframe 中,如何根据列值过滤行,进行计算并将结果分配给新列? - In a pandas dataframe, how can I filter the rows based on a column value, do calculation and assign the result to a new column? 如何根据条件替换 pandas DataFrame 中的值? - How do I replace a value in pandas DataFrame based on a condition? Pandas DataFrame 根据多个条件分组添加新列值 - Pandas DataFrame add new column values based on group by multiple conditions Pandas:添加新列并按条件从另一个dataframe赋值 - Pandas: Add new column and assigning value from another dataframe by condition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM