当前位置 : 首页 » 互动问答 » 正文

如何删除pandas列Python中的字符串列表中的重复项

分类 : 互动问答 | 发布时间 : 2018-04-27 15:05:45 | 评论 : 2 | 浏览 : 17 | 喜欢 : 0

我正尝试删除Pandas DataFrame中一列中字符串列表中的重复字符串。

例如;列表值:

 [btc,btc,btc]

Should be;

[btc]

然而,我尝试了多种方法,似乎没有任何工作,因为我无法访问列表中的字符串值。任何帮助非常感谢。

DataFrame:

 dollar_sign followers_count \
0 [btc] 35946
1 [btc] 35946
2 [btc] 35946
3 [nav] 35946
4 [btc,btc,btc] 35946

访问df_twitter ['dollar_sign']中行的列

下的字符串列表:
    打印行

Output:

 [BTC]
[BTC]
[BTC]
[NAV]
[btc,btc,btc]

回答(2)

  • 1楼
  • 你可以使用集合。例如,在df_twitter ['dollar_sign']中保持输出的风格:

    for row:     打印列表(set(row))

     [BTC]
    [BTC]
    [BTC]
    [NAV]
    [BTC]

    Output:

    [btc]
    [btc]
    [btc]
    [nav]
    [btc]
    
  • 2楼
  • 您可以使用list with map , and set获取唯一值

     df ['dollar_sign'] = list(map(set,df ['dollar_sign']))
    DF
    出[1068]:
      dollar_sign followers_count
    0 {btc} 35946
    1 {btc} 35946
    2 {btc} 35946
    3 {nav} 35946
    4 {btc} 35946

相关阅读:

raw_input function in Python

Remove duplicates from an array of objects in JavaScript

How to import the class within the same directory or sub directory?

How to return dictionary keys as a list in Python?

How to return dictionary keys as a list in Python?

Python `if x is not None` or `if not x is None`?

Assign output of os.system to a variable and prevent it from being displayed on the screen

How can I reverse a list in Python?

How can I reverse a list in Python?

What are the differences between type() and isinstance()?