問題

我有以下結構的資料幀:

 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’els’_3rdParty’.資料幀最終應如下所示:

enter image description here

  最佳答案

測試值與in行之間,並應用於單獨處理每行:

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

或使用具有列表理解的zip:

 m = [a in b for a, b in zip(df['source'], df['website'])]
 

然後透過 numpy.where 新增新值:

 df['type'] += np.where(m, '_1stParty',  '_3rdParty')
#'long' alternative
#df['type'] = df['type'] + np.where(m, '_1stParty',  '_3rdParty')
print (df)
        website            type         source
0       bbc.com  image_1stParty            bbc
1       cnn.com  audio_3rdParty         google
2    google.com  image_3rdParty  stackoverflow
3  facebook.com  video_1stParty       facebook
 

  相同標籤的其他問題

pythonpandasdataframe