Ион Сынкетру Ион Сынкетру - 1 month ago 22
Python Question

Cleaning and grouping values

df = pd.read_csv(file, sep=',', encoding='ISO-8859-1')

column_names = list(df.columns)
col_mapping = {'Sex ':'Sex', 'Fatal (Y/N)': 'Fatal', 'Species ' : 'Species'}
df = df.rename(columns=col_mapping, copy=False)


I've transformed the data as that the values from df['Age'] column that contains letter or other symbols take the value NaN

df['Age'] = np.where(pd.to_numeric(df['Age'], 'coerce').notnull(), df['Age'], NaN)


I've tried to use
df.dropna(df.Age)
to clean the
NaN
values but it gives me: TypeError: 'Series' objects are mutable, thus they cannot be hashed

I want to know how to set this problem and how to group and count the resulting values bu range (i.e 18 - 25 years : 215, 25 - 50 : 300) for future ploting

Answer

you want to use the parameter subset

df = df.dropna(subset=['Age'])
Comments