1

My Dataset has many columns. Here are two:

Index  Graduated  Age
0      College    24
1      HighSch    18
2      College    26
3      College    Nan
4      HighSch    20

The mean of Age is simple enough:

df.Age.mean()

However, I have many other columns, therefore I'm using agg():

df.groupby('Graduated').agg({'Age':'mean'})

The error I get:

No numeric types to aggregate If I insert a number instead of NaN, it works!!

Does the agg() function not allow us to run the mean if column has NaN values? Is there a way around that?

2
  • 2
    By the looks of it, it is not the "number" nan but instead a string "Nan". Change it to np.nan from numpy and it should work. Commented Jul 17, 2017 at 0:06
  • 1
    See stackoverflow.com/questions/25039328/… for an answer to your particular question not including the "Nan" issue mentioned by @ayhan Commented Jul 17, 2017 at 0:11

1 Answer 1

1

As @ayhan said, the Nan values look like strings. One possible solution is that you can replace the Nan strings you have with actual NaN values using either of those two lines:

df['Age'] = df['Age'].replace(r'Nan', np.nan, regex=True)

@ayhan's suggestion is to use to_numeric method.

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

Then execute the aggregation that you mentioned in your question. And I would do the same for all columns to avoid confusion and get things straight from the beginning for analysis purposes in the future.

Sign up to request clarification or add additional context in comments.

7 Comments

You might still need df["age"] = pd.to_numeric(df["age"], errors="coerce") if the dtype is object.
I tried it and gave me, ValueError: Unable to parse string "Nan" at position 3
That worked, would you like me to add it to the answer?
Sure that would be nice.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.