Ион Сынкетру Ион Сынкетру - 3 years ago 203
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
to clean the
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 Source

you want to use the parameter subset

df = df.dropna(subset=['Age'])
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download