【问题标题】:How to remove duplicate values in rows and add as list如何删除行中的重复值并添加为列表
【发布时间】:2022-08-05 13:30:38
【问题描述】:

我的 csv 文件:

Measure Names
State
WFH
Audit
State
WFH
Audit
State
WFH
Audit
State
WFH
Audit
State
WFH
Audit
State
WFH
Audit

我的预期输出:

List=[\'State\',\'WFH\',\'Audit\']

我不想使用 Pandas 库..我只允许使用默认包。

    标签: python list csv


    【解决方案1】:
    import pandas as pd
    
    s = """Measure Names
    State
    WFH
    Audit
    State
    WFH
    Audit
    State
    WFH
    Audit
    State
    WFH
    Audit
    State
    WFH
    Audit
    State
    WFH
    Audit"""
    with open ('file.csv', 'w') as f:
        f.write(s)
        
    result = list(set(pd.read_csv('file.csv')['Measure Names']))
    print(result)
    

    印刷

    ['WFH', 'State', 'Audit']
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-21
      • 1970-01-01
      • 1970-01-01
      • 2013-08-28
      • 1970-01-01
      • 2012-07-17
      • 1970-01-01
      • 2015-09-02
      相关资源
      最近更新 更多