Thomas Matthew Thomas Matthew -4 years ago 117
Python Question

Cannonical Approach to Combine Boolean Pandas Datarames

I am trying to combine three boolean dataframes into one, showing

True
for a dataframe that is also
True
in at least one other dataframe of the 3 that I have. Is there a more canonnical way to do this than what I have below?

# sample data
df_1 = pd.DataFrame({'gene': ['one','two','three'],
'A': [1, 0, 0],
'B': [1, 0, 0],
'C': [1, 0, 0]})
df_1.set_index('gene', inplace = True)

df_2 = pd.DataFrame({'gene': ['one','two','three'],
'A': [0, 1, 0],
'B': [0, 1, 0],
'C': [0, 1, 0]})
df_2.set_index('gene', inplace = True)

df_3 = pd.DataFrame({'gene': ['one','two','three'],
'A': [0, 0, 1],
'B': [0, 0, 1],
'C': [0, 0, 1]})
df_3.set_index('gene', inplace = True)

# combine dataframes
combined = ((df_1 > 0) | (df_2 > 0) | (df_3 > 0))


Is there a more canonical way to combine dataframes?

Answer Source

A simplified version:

 df_1  | df_2  | df_3 

Yet another version: (not dealing with performance here, just variations)

sum([df_1, df_2, df_3])
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download