【问题标题】:How to optimize below code to find the duplicate record county using pandas dataframe如何使用 pandas 数据框优化以下代码以查找重复记录县
【发布时间】:2017-09-24 11:34:17
【问题描述】:

如何优化以下代码以使用 pandas 数据框查找重复记录县

        for Duprng in range(7):
            if (df['Name'].iloc[data_row] == df['Name'].iloc[Rowcnt]):
               TtlDup = TtlDup + 1
            Rowcnt = Rowcnt + 1 
        print(TtlDup)  

名称 1.拉胡尔 2.拉维 3.拉胡尔 4.拉贾 5.内存 6.山姆 7.特州 8.大师 9.拉吉斯 10.亚杰

【问题讨论】:

  • 请提供样本数据。
  • 请详细说明您的问题

标签: python pandas dataframe


【解决方案1】:

有一个查找重复的功能:DataFrame.duplicated('column_name')

代码:

import pandas as pd
a = {'name': 'John'}
b = {'name': 'Terry'}
c = {'name': 'John'}
df = pd.DataFrame([a, b, c])

print(df)
print('\n')

df['duplicated'] = df.duplicated('name')

print('All duplicates: ')
print(df)

print('\n')

print('Count: ')
print(df['duplicated'].value_counts()) # Counts the number of False (not duplicated) and True (duplicated)

输出:

    name
0   John
1  Terry
2   John


All duplicates: 
    name  duplicated
0   John       False
1  Terry       False
2   John        True


Count: 
False    2
True     1
Name: duplicated, dtype: int64

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多