2016-05-12 62 views
2
1 0 0 0 1 
0 0 0 0 0 
0 1 0 0 1 
1 0 0 0 1 
0 0 0 0 0 
1 0 0 0 1 

我有一個數據幀(參見上面)。我需要比較它的行來獲得匹配的行。所以對於上面的df,我應該比較後得到row1 = row4 = row6和row2 = row5。有沒有任何有效的方式來做這種python行比較。如何比較Python中的數據幀的行數是否相等

回答

2

用途:

import pandas as pd 


df = pd.DataFrame({0: {0: 1, 1: 0, 2: 0, 3: 1, 4: 0, 5: 1}, 
        1: {0: 0, 1: 0, 2: 1, 3: 0, 4: 0, 5: 0}, 
        2: {0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0}, 
        3: {0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0}, 
        4: {0: 1, 1: 0, 2: 1, 3: 1, 4: 0, 5: 1}}) 
print df 
    0 1 2 3 4 
0 1 0 0 0 1 
1 0 0 0 0 0 
2 0 1 0 0 1 
3 1 0 0 0 1 
4 0 0 0 0 0 
5 1 0 0 0 1 
#first select only all duplicated rows 
df1 = df[df.duplicated(keep=False)] 
print df1 
    0 1 2 3 4 
0 1 0 0 0 1 
1 0 0 0 0 0 
3 1 0 0 0 1 
4 0 0 0 0 0 
5 1 0 0 0 1 

#sort values by all columns 
df2 = df1.sort_values(by=df.columns.tolist()) 
print df2 
    0 1 2 3 4 
1 0 0 0 0 0 
4 0 0 0 0 0 
0 1 0 0 0 1 
3 1 0 0 0 1 
5 1 0 0 0 1 

#find groups 
print (~((df2 == df2.shift(1)).all(1))).cumsum() 
1 1 
4 1 
0 2 
3 2 
5 2 
dtype: int32 
#print groups  
for i, g in df.groupby((~((df2 == df2.shift(1)).all(1))).cumsum()): 
    print g 

    0 1 2 3 4 
1 0 0 0 0 0 
4 0 0 0 0 0 
    0 1 2 3 4 
0 1 0 0 0 1 
3 1 0 0 0 1 
5 1 0 0 0 1 

#dict comprehension for storing groups 
dfs = {i-1: g for i,g in df.groupby((~((df2 == df2.shift(1)).all(1))).cumsum())} 
print dfs 
{0.0: 0 1 2 3 4 
1 0 0 0 0 0 
4 0 0 0 0 0, 1.0: 0 1 2 3 4 
0 1 0 0 0 1 
3 1 0 0 0 1 
5 1 0 0 0 1} 

print dfs[0] 
    0 1 2 3 4 
1 0 0 0 0 0 
4 0 0 0 0 0 

print dfs[1] 
    0 1 2 3 4 
0 1 0 0 0 1 
3 1 0 0 0 1 
5 1 0 0 0 1 
1

這是我會怎麼想做到這一點。

import pandas as pd 


df = pd.DataFrame({0: {0: 1, 1: 0, 2: 0, 3: 1, 4: 0, 5: 1}, 
        1: {0: 0, 1: 0, 2: 1, 3: 0, 4: 0, 5: 0}, 
        2: {0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0}, 
        3: {0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0}, 
        4: {0: 1, 1: 0, 2: 1, 3: 1, 4: 0, 5: 1}}) 

groups = df.groupby(df.columns.tolist()) 
df.loc[:, 'group_num'] = None 


for num, group in enumerate(groups): 
    df.loc[group[1].index, 'group_num'] = num 

產量...

0 1 2 3 4 group_num 
0 1 0 0 0 1   2 
1 0 0 0 0 0   0 
2 0 1 0 0 1   1 
3 1 0 0 0 1   2 
4 0 0 0 0 0   0 
5 1 0 0 0 1   2 

爲什麼組的最後一行[1]?

因爲您正在迭代表單(group_name,group_table)的元組。組[1]訪問實際分組的數據幀。