【问题标题】:Extract duplicate values from all the column of csv file by using python使用python从csv文件的所有列中提取重复值
【发布时间】:2017-12-13 20:49:11
【问题描述】:

我有一个 csv 文件,其中有 一个唯一列 (id) 和 7 个其他列 (C1) 由与一个唯一列相关的类组成。现在我想只从 7 列中提取重复数据 但我实际上需要编写类似于输出的代码。

例子:

**id C1   C2 C3 C4 C5 C6 C7**  
P1   a1   a2    a4  
P2   a2         a4    a6  a7  
P3                 a5     a7  
P4   a1         a3   

输出

a1=> P1, P4  
a2=>P1, P2  
a3=>P4  
a4=>P1, P2  
a5=>P3  
and so on

【问题讨论】:

    标签: python database pandas csv


    【解决方案1】:

    stackgroupby 一起使用

    df.set_index('id').stack().reset_index().groupby(0).id.apply(list)
    Out[137]: 
    0
    a1    [P1, P4]
    a2    [P1, P2]
    a3        [P4]
    a4    [P1, P2]
    a5        [P3]
    a6        [P2]
    a7    [P2, P3]
    Name: id, dtype: object
    

    【讨论】:

    • 哇,我的文件名是 go.csv,你能分享一个如何阅读它的代码,然后应用上述过程。对不起,我是新手
    • @user3768070 你可以做价值计数:value_counts Like df.set_index('id').stack().reset_index().groupby(0).id.apply(list).astype(str).value_counts()
    猜你喜欢
    • 2021-05-22
    • 1970-01-01
    • 2018-04-20
    • 2022-01-05
    • 2021-12-19
    • 1970-01-01
    • 1970-01-01
    • 2016-02-03
    • 2019-05-30
    相关资源
    最近更新 更多