Home > Mobile >  How to do a majority voting on three dataframe in python
How to do a majority voting on three dataframe in python

Time:06-10

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

1 0 1 0

0 0 1 0

0 1 0 0

0 1 0 1

CodePudding user response:

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
  • Related