简体   繁体   中英

Elements in common for each two elements in first column dataframes

I'm new with Pandas. I have the following dataframe.

 Group type G1 a1 G1 a2 G1 a3 G2 a2 G2 a1 G3 a1 G4 a1 G5 a4 G5 a1 

And I would like to obtain for each couple of groups how many "types" they have in common. Something like this:

 Group type count G1 a1 G1 a2 G1 a3 G2 a2 G2 a1 G3 a1 G4 a1 G5 a4 G5 a1 count: (G1, G2, 2) (Elements in common: a1,a2) count: (G1, G3, 1) (Elements in common: a1) count: (G1, G4, 1) (Elements in common: a1) ... 

Do you have any idea how could I implement this? Is there any function from the pandas library that could guide me into the right direction.

I think you need numpy.intersect1d :

import itertools

#get all combinations of Group values
c = list(itertools.combinations(list(set(df['Group'])), 2))

df = df.set_index('Group')

#create list of tuples of intersections and lengths 
L = []
for a, b in c:
    d = np.intersect1d(df.loc[a], df.loc[b]).tolist()
    L.append((a,b, len(d), d))

#new DataFrame
df = pd.DataFrame(L, columns=['a','b','lens','common'])
print (df)
    a   b  lens    common
0  G2  G4     1      [a1]
1  G2  G1     2  [a1, a2]
2  G2  G3     1      [a1]
3  G2  G5     1      [a1]
4  G4  G1     1      [a1]
5  G4  G3     1      [a1]
6  G4  G5     1      [a1]
7  G1  G3     1      [a1]
8  G1  G5     1      [a1]
9  G3  G5     1      [a1]

Given a dataframe:

import pandas as pd
df = pd.DataFrame([['G1', 'G1', 'G2', 'G2'], ['a1', 'a2', 'a1', 'a3']]).T
df.columns = ['group', 'type']

Then there are two options:

df.groupby('type').count()

or if you want to know them explicitly:

df.groupby(['type', 'group']).count()

Thus you can do, eg:

df1.loc['a1']

with output:

group
G1
G2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM