【问题标题】:Retain only the first word if the string contains duplicate words如果字符串包含重复的单词,则只保留第一个单词
【发布时间】:2021-03-07 02:31:01
【问题描述】:
customer_name                                               ANDY
number_of_product_variants                                      2
number_of_channels                                              1
number_of_discount_codes                                        1
order_count                                                     1
order_name                                            #1100,#1100
discount_code                        Christmas2020, Christmas2020
channel                                      Instagram, Instagram
product_variant                    Avengers Set A, Avengers Set B

仅当字符串包含重复项时,我才想删除重复的单词。

预期输出:

customer_name                                                ANDY
number_of_product_variants                                      2
number_of_channels                                              1
number_of_discount_codes                                        1
order_count                                                     1
order_name                                                  #1100
discount_code                                       Christmas2020
channel                                                 Instagram
product_variant                    Avengers Set A, Avengers Set B

我试过的代码:

def unique_string(l):
    ulist = []
    [ulist.append(x) for x in l if x not in ulist]
    return ulist

customer_df['channel_2']=customer_df['channel']
customer_df['channel_2'].apply(unique_string)

将下面的代码仅用于channel 列会返回:

0                                   [S, e, a, r, c, h, ,]
1                    [P, a, i, d,  , A, s, :, S, o, c, l]
2                 [P, a, i, d,  , A, s, :, S, o, c, l, ,]
3                                      [U, n, k, o, w, ,]
```

【问题讨论】:

    标签: python python-3.x pandas list


    【解决方案1】:

    您的数据框似乎包含表示列表而不是列表的字符串。

    例子:

    '[ "Instagram", "Instagram" ]' and not ["Instagram", "Instagram"]
    

    注意外面的单引号。

    您可以看到,因为 for 理解似乎迭代字符串的字符而不是列表的元素。

    要将列表的字符串表示形式转换为字符串,您应该首先使用:

    import ast
    customer_df["channel"] = customer_df["channel"].apply(ast.literal_eval) 
    

    如果您想了解更多关于 ast.literal_eval 的信息,请参考this 问题。

    然后你可以应用你的函数 unique_string。

    【讨论】:

    • 你能解释一下为什么在上面给出的问题中需要这样做吗?我无法理解为什么我们需要literal_eval。
    【解决方案2】:

    如果多个值的顺序不重要,您可以将set 与, 拆分的值一起使用。

    如果顺序很重要,请使用带有.keys() 的dict:

    customer_df = pd.DataFrame({"channel_2":['Instagram, Instagram',
                                             'Instagram, Instagram1, Instagram, Instagram2']})
        
    f1 = lambda x: ', '.join(set(y for y in x.split(', ')))
    f2 = lambda x: ', '.join(dict.fromkeys(y for y in x.split(', ')).keys())
    
    customer_df['channel_2_1'] = customer_df['channel_2'].apply(f1)
    customer_df['channel_2_2'] = customer_df['channel_2'].apply(f2)
    print (customer_df)
                                          channel_2  \
    0                          Instagram, Instagram   
    1  Instagram, Instagram1, Instagram, Instagram2   
    
                             channel_2_1                        channel_2_2  
    0                          Instagram                          Instagram  
    1  Instagram2, Instagram1, Instagram  Instagram, Instagram1, Instagram2  
    

    【讨论】:

    • @Luc - 取决于数据,如果使用Instagram, Instagram1, Instagram, Instagram2,输出应该不同(仅排序)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-13
    • 1970-01-01
    • 2020-02-11
    • 1970-01-01
    相关资源
    最近更新 更多