-1

I have a list number data frame and I want to calculate the average using Python language enter image description here

3
  • Does this answer your question? Pandas mean of list within dataframe Commented Dec 21, 2021 at 12:20
  • You want to calculate the average within each semester, I suppose? Commented Dec 21, 2021 at 12:24
  • Please provide enough code so others can better understand or reproduce the problem. Commented Dec 29, 2021 at 23:20

1 Answer 1

0

You can use .apply here with a lambda function.

df["Course Final Score"] = df["Course Final Score"].apply(lambda x: sum(x)/len(x))

Essentially your applying the function sum(x)/len(x) to every x in your column "Course Final Score", i.e replacing the list with the sum of that list divided by the length of that list which will give you the mean.

EDIT

If your list contains a string of a list, you can use json to decode it as follows:

import json
df["Score"].apply(lambda x: sum(json.loads(x))/len(json.loads(x)))
Sign up to request clarification or add additional context in comments.

7 Comments

TypeError: unsupported operand type(s) for +: 'int' and 'str'
Does your list of Scores contain strings at any point?
I've edited a solution for if your scores are string values.
ValueError: could not convert string to float: '['
ValueError: could not convert string to float: '['
|

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.