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, ,]
```
如果多个值的顺序不重要,则可以使用
set
将值按,
分割如果顺序很重要,请将dict与
.keys()
一起使用:您的数据框似乎包含表示列表而不是列表的字符串
例如:
注意外部的单引号
您可以看到这一点,因为for construction似乎迭代字符串的字符,而不是列表的元素
要将列表的字符串表示形式转换为字符串,应首先使用:
如果您想了解有关ast.literal_eval的更多信息,请参阅this问题
然后,您可以应用您的函数unique_字符串
相关问题 更多 >
编程相关推荐