Pergunta

Lets say I have 2 pivot tables and I merge them, to make the case simple I will use binary digits. The code below works:

import pandas as pd

df=pd.DataFrame({'axis1': ['A','B','C','D'],
                 'A': [1,0,1,1],
                 'B': [1,0,0,0],
                 'C': [0,1,0,1],
                 'D': [1,0,1,0],
                 }).set_index(['axis1'])


df2=pd.DataFrame({'axis1': ['A','E','G','D'],
                 'E': [1,0,0,1],
                 'B': [1,1,0,0],
                 'C': [1,1,0,1],
                 'F': [1,0,1,0],
                 }).set_index(['axis1'])

print (df)
print (df2)

df3 = pd.concat((df, df2)).fillna(0)
g=df3.groupby(df3.index)
g=g.sum()
print(g)

Is there a way to label all the data to strings like this 1='Y', 0='N', 2='M'

When combining 2 tables with numbers that are NOT binary digits (ie. 5, 3, 2.5) is there a way to separate out which data is exclusively from df, exclusively from df2, and which is from a combination of df1+df2?

For example in the pic below how would I separate the data into 3 groups red, purple, and blue from the two dataframes.

enter image description here

Foi útil?

Solução

In [7]: replacers = {1: '1=Y', 0: '0=Y', 2: '2=M'}

In [10]: g.replace(replacers)
Out[10]: 
         A    B    C    D    E    F
axis1                              
A      1=Y  2=M  1=Y  1=Y  1=Y  1=Y
B      0=Y  0=Y  1=Y  0=Y  0=Y  0=Y
C      1=Y  0=Y  0=Y  1=Y  0=Y  0=Y
D      1=Y  0=Y  2=M  0=Y  1=Y  0=Y
E      0=Y  1=Y  1=Y  0=Y  0=Y  0=Y
G      0=Y  0=Y  0=Y  0=Y  0=Y  1=Y

[6 rows x 6 columns]

I'm not sure what your second question means. Perhaps you could give an example with your expected output?

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top