1 回答

TA貢獻(xiàn)1850條經(jīng)驗(yàn) 獲得超11個(gè)贊
重構(gòu)您的函數(shù)以對(duì)行進(jìn)行操作,然后axis=1在調(diào)用應(yīng)用程序時(shí)使用:
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]:
添加回答
舉報(bào)