Python 在一个数据帧内,根据重复和多个重复子集进行排序
我是python新手,我想根据数据帧集中的某列数据对一些重复数据进行排序,例如Python 在一个数据帧内,根据重复和多个重复子集进行排序,python,pandas,dataframe,Python,Pandas,Dataframe,我是python新手,我想根据数据帧集中的某列数据对一些重复数据进行排序,例如 Import pandas as pd df = pd.read_excel('Data.xlsx', index = ['ID'] df2 = df[df.duplicated(subset = ['A','B'], keep = False)] print (df2) 假设输出是这样的 'ID'|'Name' |'A'|'B'| 1 | Ash | 1 | 1 | 2 | James | 1 | 1
Import pandas as pd
df = pd.read_excel('Data.xlsx', index = ['ID']
df2 = df[df.duplicated(subset = ['A','B'], keep = False)]
print (df2)
假设输出是这样的
'ID'|'Name' |'A'|'B'|
1 | Ash | 1 | 1 |
2 | James | 1 | 1 |
3 | Ash | 1 | 1 |
4 | James | 1 | 1 |
5 | Ash | 2 | 1 |
6 | James | 1 | 1 |
7 | Ash | 2 | 1 |
我希望数据输出如下:
'Name' |'A'|'B'|'Pattern'|'Frequency of Pattern'|
Ash | 1 | 1 | 1 | 2 |
Ash | 2 | 1 | 2 | 2 |
James | 1 | 1 | 3 | 3 |
到目前为止,我还没有发现任何类似的帖子用于计数重复项,然后通过以下方式将新列添加到特定位置:
df4 = df3.groupby(['Name','A','B']).size().reset_index(name='Frequency of Pattern')
df4.insert(3, 'Pattern', df4.index + 1)
print (df4)
Name A B Pattern Frequency of Pattern
0 Ash 1 1 1 2
1 Ash 2 1 2 2
2 James 1 1 3 3