1 回答
TA貢獻1850條經驗 獲得超11個贊
重構您的函數以對行進行操作,然后axis=1在調用應用程序時使用:
def f(row):
#print(row.name,row.col1,row.col2)
value = None
if 'apple' in row.col1.lower():
idx = row.col1.lower().split().index('apple')
# print(row.col2[idx])
value = row.col2[idx]
return value
df['col3' ] = df.apply(f,axis=1)
使用您的示例 DataFrame:
In [34]: print(df.to_string())
col1 col2 col3
0 The weight of a apple [DET, NOUN, ADP, DET, NOUN] NOUN
1 Apple MacBook release date news and rumors [PROPN, NOUN, NOUN, NOUN, CCONJ, PROPN] PROPN
In [35]:
添加回答
舉報
