Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to do a majority voting on three dataframe in python

I have three Dataframe (dataframe1, dataframe2, dataframe3)

import pandas as pd
data1= [[0, 0, 1, 1], [1,0,1,0], [0,1,1,0], [1,1,0,1]]
dataframe1 = pd.DataFrame(data1, columns=['0', '1', '2', '3'])
dataframe1
data2 = [[1,0,1,0], [0,0,1,0], [0,1,0,1], [0,0,0,1]]
dataframe2 = pd.DataFrame(data2, columns=['0', '1', '2', '3'])
dataframe2
data3 = [[1,0,1,0], [0,0,1,0], [0,0,0,0], [0,1,0,1]]
dataframe3 = pd.DataFrame(data3, columns=['0', '1', '2', '3'])
dataframe3

I need a new data frame based on the majority element on each position of above three dataframe

Expected outcome

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

1 0 1 0

0 0 1 0

0 1 0 0

0 1 0 1

>Solution :

Because same index values in each DataFrame is possible use concat and then per indices get majority element by Series.mode, if possible multiple majority elements and need one select first value by Series.iat:

df = (pd.concat([dataframe1, dataframe2, dataframe3])
        .groupby(level=0)
        .agg(lambda x: x.mode().iat[0]))
print (df)
   0  1  2  3
0  1  0  1  0
1  0  0  1  0
2  0  1  0  0
3  0  1  0  1
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading