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

时间:2017-09-24 11:34:18

标签: python pandas dataframe

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

String shareBody = "content";
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "title"));

名称 1.Rahul 2.Ravi 3.Rahul 4.Raja 5.Ram 6.Sam 7.Teju 8.Guru 9.Rajith 10.Yaj

1 个答案:

答案 0 :(得分:0)

有一个查找重复项的功能: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