用python数据帧中的新结尾替换单词的结尾
发布时间:2020-12-20 11:02:45 所属栏目:Python 来源:网络整理
导读:我有一个充满法语单词,结尾和新结尾的Dataframe.我想创建第4列,替换为单词,如下所示: word |ending|new ending|what i want|--------------------------------------placer |cer |ceras |placeras |placer |cer |cerait |placerait |placer |cer |ceront |p
我有一个充满法语单词,结尾和新结尾的Dataframe.我想创建第4列,替换为单词,如下所示:
word |ending|new ending|what i want| -------------------------------------- placer |cer |ceras |placeras | placer |cer |cerait |placerait | placer |cer |ceront |placeront | finir |ir |iras |finiras | 所以它基本上是在第1列中用第3列中的内容替换第2列中的等价物. 有任何想法吗 ? 解决方法
使用
apply() :
df['new_word'] = df.apply( lambda row: row['word'].replace(row['ending'],row['new ending']),axis=1 ) # word ending new ending new_word #0 placer cer ceras placeras #1 placer cer cerait placerait #2 placer cer ceront placeront #3 finir ir iras finiras 正如@jpp指出的那样,对这种方法的一个警告是,如果结尾存在于字符串的中间,它将无法正常工作. 在这种情况下,请参阅this post,了解如何在字符串末尾进行更换. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |