Sam Vamsi
Sam Vamsi

Reputation: 85

How to calculate mean by skipping String Value in Numeric Column?

Name  Emp_ID     Salary  Age
0 John   Abc     21000   31
1 mark   Abn     34000   82
2 samy   bbc     thirty  78
3 Johny  Ajc     21000   34
4 John   Ajk    2100.28  twentyone

How to calculate mean of 'Age' Column without changing string value in that column. Basically i want to loop through age column for numerical value and gives mean of that list of value. If any string comes it should skip that value?

Upvotes: 1

Views: 1112

Answers (1)

Erfan
Erfan

Reputation: 42906

Use pd.to_numeric with the argument errors='coerce', which turns values to NaN if it can't convert it to numeric. Then use Series.mean:

pd.to_numeric(df['Age'],errors='coerce').mean()

#Out
56.25

Upvotes: 1

Related Questions