user16239103
user16239103

Reputation:

Comparing counts in rows from a Dataframe with Pandas, Python

I'm trying to obtain the most common asnwers so we have Yes/No questions and it has i eleven questions from this one I would like to know from Yes/No which was has most answers as an example:

If in more than the half of the eleven i's has No>Yes the most common answers will be 'NO' but I'm not really sure what function I need to do this, comparing the rows 0 and 1 to know which one had more answers.

I would like to do this kind of script:

if row[0] > row1: print("No is the most common answer")

This is the example data that I have:

  index i1  i2  i3  i4  i5  i6      i7      i8  i9  i10 i11 
0   No  94  123 96  108 122 106.0   95.0    124 104 118 73  
1   Yes 34  4   33  21  5   25.0    34.0    5   21  9   55

EDIT: Wanted result for using just the first column.

enter image description here

Upvotes: 2

Views: 65

Answers (1)

cmauck10
cmauck10

Reputation: 163

To obtain a list of 'yes' and 'no' you can do:

no_count = df.iloc[0].values[1:]
yes_count = df.iloc[1].values[1:]
most_common = ['no' if no_count[i]>yes_count[i] else 'yes' for i in range(len(no_count))]

Then you can count the number of each

number_no = most_common.count("no")
number_yes = most_common.count("yes")

Upvotes: 1

Related Questions