熊猫:如何通过比较其他列值来修改数据框中列的值

时间:2019-09-29 05:50:49

标签: python pandas dataframe

我的数据框具有以下结构:

raw_data = {'website': ['bbc.com', 'cnn.com', 'google.com', 'facebook.com'], 
    'type': ['image', 'audio', 'image', 'video'], 
    'source': ['bbc','google','stackoverflow','facebook']}
df = pd.DataFrame(raw_data, columns = ['website', 'type', 'source']) 

enter image description here

我要修改type列中的值,条件是如果source中存在website,则后缀type带有'_1stParty'否则为'_3rdParty '。数据框最终应该看起来像:

enter image description here

3 个答案:

答案 0 :(得分:1)

测试值用int32包围行,并分别申请处理每行:

MyCoolInt32

或者将int32与列表理解一起使用:

in

,然后通过numpy.where添加新值:

m = df.apply(lambda x: x['source'] in x['website'], axis=1)

答案 1 :(得分:0)

您可以像这样使用apply方法

df["type"] = df.apply(lambda row: f"{row.type}_1stparty" if row.source in row.website \
                      else f"{row.type}_thirdparty", axis=1)
df

答案 2 :(得分:0)

此解决方案必须比使用apply()的解决方案更快:

df.type += df.website.str.split('.').str[0].eq(df.source).\
           replace({True: '_1stParty', False: '_3rdParty'})
相关问题