Python Dataframe: Build majority class based on other column values

Viewed 15

from a data annotation project I got following dataframe, which is called "anno_all":

       id   Embedded_text                             Sentiment Sentiment2  Sentiment3
 0  23395   Unglaublich. Ich hatte @JanJosefLiefers noch...     2   2   2
 1  29600   Kicking of our @EPPGroup bureau meeting in R...     3   3   1
 2  47788   Die Schulbürokratie hält mit der #Corona-Herau...   2   2   3
 3  44016   "Es wird keine Individualisierung von Risiken ...   3   3   4
 4  35878   "Diese Regierung hat das Vertrauen der Bevölke...   1   2   3

The annotators could give labels like "1", "2", "3" or "4". Now I want to create a new column which describes the majority of the labeled data. If there is no majority I want the value to be "no_majority" or "-1". So the resulting dataframe should look like this:

       id   Embedded_text                             Sentiment Sentiment2  Sentiment3  majority_sentiment
 0  23395   Unglaublich. Ich hatte @JanJosefLiefers noch...     2   2   2                  2
 1  29600   Kicking of our @EPPGroup bureau meeting in R...     3   3   1                  3
 2  47788   Die Schulbürokratie hält mit der #Corona-Herau...   2   2   3                  2
 3  44016   "Es wird keine Individualisierung von Risiken ...   3   3   4                  3
 4  35878   "Diese Regierung hat das Vertrauen der Bevölke...   1   2   3                  no_majority

Does anyone know how to solve this problem? I tried something like this, but this seems to be too long in code for every case and I think that there is a shorter way. Also I am not too sure how to deal with no majority here:

anno_all.loc[(anno_all['Sentiment'] == 2) & (anno_all["Sentiment2"] == 2)& (anno_all["Sentiment3"] == 2), 'majority_sentiment'] = 2
anno_all.loc[(anno_all['Sentiment'] == 1) & (anno_all["Sentiment2"] == 1)& (anno_all["Sentiment3"] == 1), 'majority_sentiment'] = 1
anno_all.loc[(anno_all['Sentiment'] == 3) & (anno_all["Sentiment2"] == 3)& (anno_all["Sentiment3"] == 3), 'majority_sentiment'] = 3
anno_all.loc[(anno_all['Sentiment'] == 4) & (anno_all["Sentiment2"] == 4)& (anno_all["Sentiment3"] == 4), 'majority_sentiment'] = 4

anno_all.loc[(anno_all['Sentiment'] == 1) & (anno_all["Sentiment2"] == 1), 'majority_sentiment'] = 1
anno_all.loc[(anno_all['Sentiment'] == 1) & (anno_all["Sentiment3"] == 1), 'majority_sentiment'] = 1
anno_all.loc[(anno_all['Sentiment2'] == 1) & (anno_all["Sentiment3"] == 1), 'majority_sentiment'] = 1

anno_all.loc[(anno_all['Sentiment'] == 2) & (anno_all["Sentiment2"] == 2), 'majority_sentiment'] = 2
anno_all.loc[(anno_all['Sentiment'] == 2) & (anno_all["Sentiment3"] == 2), 'majority_sentiment'] = 2
anno_all.loc[(anno_all['Sentiment2'] == 2) & (anno_all["Sentiment3"] == 2), 'majority_sentiment'] = 2

anno_all.loc[(anno_all['Sentiment'] == 3) & (anno_all["Sentiment2"] == 3), 'majority_sentiment'] = 3
anno_all.loc[(anno_all['Sentiment'] == 3) & (anno_all["Sentiment3"] == 3), 'majority_sentiment'] = 3
anno_all.loc[(anno_all['Sentiment2'] == 3) & (anno_all["Sentiment3"] == 3), 'majority_sentiment'] = 3

anno_all.loc[(anno_all['Sentiment'] == 4) & (anno_all["Sentiment2"] == 4), 'majority_sentiment'] = 4
anno_all.loc[(anno_all['Sentiment'] == 4) & (anno_all["Sentiment3"] == 4), 'majority_sentiment'] = 4
anno_all.loc[(anno_all['Sentiment2'] == 4) & (anno_all["Sentiment3"] == 4), 'majority_sentiment'] = 4
0 Answers
Related