“根据条件选择联接”

2024-05-16 20:55:48 发布

您现在位置:Python中文网/ 问答频道 /正文

喂,Python族和Python族

将我的DF列连接到其他语言(如法语、德语)的单词列表的最佳方式是什么

基本上,使用country列,我想用不同的列表加入我的OneCol。我在下面编写了代码,但它返回一个空列。我认为逻辑是好的,但也许我遗漏了一些东西,因为我是一个初学者。我试着写一个if-elif-else语句。我的目标是清除每种语言拼写错误的字符串。非常感谢你的帮助

我的数据帧:

new_df = pd.DataFrame({
    'ID': [1,2,3, 4],
    'Country': [1,2,3,12],
    'OneCol': ['I like this', 'Je ne sais pas', 'Niet', 'keine gar nicht ']
})

我的代码:

conditions = [new_df["Country"].isin(['1','2','12']), 
              new_df["Country"]=='3', 
              new_df["Country"]=='4',  
              new_df["Country"]=='5', 
              new_df["Country"]=='6', 
              new_df["Country"]=='10']

applied_actions =  [new_df['OneCol'].apply(lambda row: ' '.join(list(set(row.split()) & set(words.words())))), 
                    new_df['OneCol'].apply(lambda row: ' '.join(list(set(row.split()) & set(list_fr)))), 
                    new_df['OneCol'].apply(lambda row: ' '.join(list(set(row.split()) & set(list_de)))),
                    new_df['OneCol'].apply(lambda row: ' '.join(list(set(row.split()) & set(list_it)))),
                    new_df['OneCol'].apply(lambda row: ' '.join(list(set(row.split()) & set(list_es)))),
                    new_df['OneCol'].apply(lambda row: ' '.join(list(set(row.split()) & set(list_se))))]


new_df['ResultG'] = np.select(conditions, applied_actions, default=None)

Tags: lambda代码语言df列表newcountryconditions