简体   繁体   English

Dataframe 根据条件替换为另一行

[英]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.

相关问题 根据条件将 Pandas DataFrame 中的一行替换为“NaN” - Replace a row in Pandas DataFrame with 'NaN' based on condition 基于另一个数据框替换熊猫中的行数据 - replace row data in pandas based on another dataframe 根据条件在一个数据帧中拆分并替换为熊猫中的另一个数据帧 - Split and replace in one dataframe based on a condition with another dataframe in pandas 根据其他列条件用数据框中的另一个列表替换列表 - Replace list with another list in dataframe based on another column condition 如何根据条件和另一行的值将 function 应用于 dataframe 行? - How to apply a function to a dataframe row based on a condition and values of another row? 将条件从一个数据帧替换为另一个长度不同的行[python] - Replace row on a condition from one dataframe to another with different lengths [python] 根据条件从另一个 dataframe 值替换列的值 - Python - Replace values of a column from another dataframe values based on a condition - Python Pandas:根据条件用另一个数据帧值替换数据帧中的值 - Pandas: replace values in dataframe with another dataframes values based on condition 根据另一列条件替换 dataframe 中的空白值 - Replace blank value in dataframe based on another column condition 根据 Pandas 中的条件将多行的值替换为另一行的值 - Replace the values of multiple rows with the values of another row based on a condition in Pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM