[英]Dataframe replace with another row, based on condition
I have a dataframe like the following:我有一个 dataframe,如下所示:
ean product_resource_id shop
----------------------------------------------------
123 abc xxl
245 bed xxl
456 dce xxl
123 0 conr
245 0 horec
I want to replace 0 "product_resource_id"
s with an id where " ean
"s are same.我想用“ ean
”相同的 id 替换0 "product_resource_id"
。
I want to get a result like:我想得到这样的结果:
ean product_resource_id shop
----------------------------------------------------
123 abc xxl
245 bed xxl
456 dce xxl
123 abc conr
245 bed horec
Any help would be really helpful.任何帮助都会非常有帮助。 Thanks in advance!提前致谢!
Idea is filter rows with 0
values in product_resource_id
, remove duplicates by ean
column if exist and create Series by DataFrame.set_index
for mapping, if no match values are replace by original by values by Series.fillna
, because non match values return NaN
s:想法是过滤product_resource_id
中具有0
值的行,如果存在则通过ean
列删除重复项,并通过DataFrame.set_index
创建 Series 进行映射,如果没有匹配值被Series.fillna
的值替换为原始值,因为不匹配值返回NaN
s:
#mask = df['product_resource_id'].ne('0')
#if 0 is integer
mask = df['product_resource_id'].ne(0)
s = df[mask].drop_duplicates('ean').set_index('ean')['product_resource_id']
df['product_resource_id'] = df['ean'].map(s).fillna(df['product_resource_id'])
print (df)
ean product_resource_id shop
0 123 abc xxl
1 245 bed xxl
2 456 dce xxl
3 123 abc conr
4 245 bed horec
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.