[英]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 == Product
按RootProduct
分组时,如何添加第四列,重复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 indexing
与Series.eq
然后创建Series
的指数与Product
由DataFrame.set_index
,因此可能使用Series.map
柱RootProduct
:
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.