【问题标题】:How to count duplicate rows and compare the two column values in excel using python如何使用python计算重复行并比较excel中的两列值
【发布时间】:2020-01-05 05:27:29
【问题描述】:

这是行和列

email                mark
A@email.com           50
B@email.com           60
B@email.com           50
B@email.com           60
B@email.com           60

这是异常输出

email                   mark    totalcount
A@email.com             50      1
B@email.com             50      1
B@email.com             60      3

这是我的python代码

df=pd.read_excel('email.xlsx')
df['Total'] = df.mark.apply(lambda x: df.mark.value_counts()[x])
dr = data_file[['email', 'mark', 'totalcount']]
print(dr)

我的输出是这样的

          email        mark    totalcount
0          A@email.com   50     2
1          B@email.com   60     3
2          B@email.com   50     2
3          B@email.com   60     3
4          B@email.com   60     3

如何比较两列并添加重复的行值。那你能帮帮我吗

【问题讨论】:

    标签: python excel pandas csv


    【解决方案1】:

    您应该同时考虑电子邮件和标记。我认为分组和转换会起作用

    df['total_count'] = df.groupby(['email', 'mark'])['mark'].transform('count')
    dr = df.drop_duplicates()
    

    输出:

          email      mark  total_count
    0  A@email.com    50            1
    1  B@email.com    60            3
    2  B@email.com    50            1
    

    【讨论】:

    • 或者只是df.groupby(['email', 'mark'],sort=False).size().reset_index(name='total_count')
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-23
    • 1970-01-01
    相关资源
    最近更新 更多