0

1.I want to count how many males and females are in my "sex" column in excel.

I tried sex_value = df.groupby("sex").size() but there are space in some of them in. eg. "F " and "F" (the same is with "M" "M ")

If everything were like "M" or "F" I would use:

sex_value = df.groupby("sex").size() 

Output:
sex
F           37
F           27
M           40
M           31
dtype: int64

In my case it should be something like

sex_value_female = df[(df['sex']=='F') & (df['sex'] == 'F ')].sum()
sex_value_male = df[(df['sex']=='M') & (df['sex'] == 'M ')].sum()

but it doesnt work.

2. The same problem is with mean value.

#mean value of brainweight and bodyweight for males and females
mean = df.groupby('sex').agg({'bodywt': 'mean', 'brainwt': 'mean'})

Output:
             bodywt     brainwt
sex                            
F         19.696216  410.059459
F         21.262963  440.122222
M         21.669750  410.030000
M         22.870968  433.709677
macaroni
  • 121
  • 1
  • 3
  • 11

1 Answers1

0

let's do strip to get ride of whitespace

df.sex = df.sex.str.strip()
sex_value = df.groupby("sex").size() 
Ehsan
  • 711
  • 2
  • 7
  • 21