0

I'm trying to calculate the mean of a column which contains a lists/series but get an error while doing so. The example dataframe as follows:

import pandas as pd
df = pd.DataFrame({'a': [[1,2,3]]})
df ['a'].mean()

The error as follows:

Could not convert [1, 2, 3] to numeric.

Not sure why this happens and how to fix it. Can someone please help? thanks

1
  • 2
    You bave a list, use df['a'].apply(np.mean) Commented Jun 7, 2020 at 14:55

1 Answer 1

3

Since your cell value type is list we need apply

df.a.apply(np.mean)
0    2.0
Name: a, dtype: float64
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! And how can I convert and store it as a list i.e. [1,2,3]. list(df['a']) seems to add extra [] whereas just assigning it to a list with mylist = df['a'] converts mylist to a series.

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.