Home > Net >  Two entry table from columns pandas
Two entry table from columns pandas

Time:11-25

I am trying to create a "two-entry table" from many columns in my df. I tried with pivot_table / crosstrab / groupby but results appeareance using this functions is not acomplish since will not be a "two entry table"

for example if i have a dataframe like this :

 df
 A  B  C  D  E  
 1  0  0  1  1        
 0  1  0  1  0      
 1  1  1  1  1      

I will like to transform my df to a df which could be seen like a "two-entry table"

   A B C D E
A  2 1 1 2 2
B  1 2 1 2 1 
C  1 1 1 1 1
D  2 2 1 3 1
E  2 1 1 1 2

so if i should explain first row, would be as A has two 1 in his column, then A-A = 2, A-B = 1 because they shared one's in the third row level in df, A-C = 1 because the third row in df they shared one's in the same row level and finaly A-E = 2 because they shared one's in the first row and the third row of df

CodePudding user response:

Use pd.DataFrame.dot with T:

df.T.dot(df) # or df.T@df

Output:

   A  B  C  D  E
A  2  1  1  2  2
B  1  2  1  2  1
C  1  1  1  1  1
D  2  2  1  3  2
E  2  1  1  2  2
  • Related