データフレームのように特定の単語を得る:Pythonのデータフレーム
stop_wd=['the','a']
df2=pandas.DataFrame({'col1':[1,3,4],
'col2':['the future','a dog','data frame']})
df2
col1 col2
0 1 the future
1 3 a dog
2 4 data frame
私が取得したい結果:
col1 col2
0 1 future
1 3 dog
2 4 data frame
stop_wd=['the','a']
のでthe future
を削除する必要がありますがthe
。 a dog
はa
を削除する必要があります。 data frame
は何も削除しません。
は私がapply
を使用しよう:
def word(x,stop_wd=stop_wd):
for r in stop_wd:
if r in x.split():
x=x.split(' ').remove(r)
return x
else:
return x
df2.col2=df2.col2.apply(word)
が、これは動作しません。私は、この結果を得る:
col1 col2
0 1 None
1 3 a dog
2 4 data frame