Issue
I was looking for a way to find the mean of numerical values based on a certain column. I looked to this link for advice but it requires all values of one column to be the same and I'm sure there's a Pythonic way that would do that for all values that are duplicates.
Here's an example.
data = {
"Name": ["John", "John", "Robert", "Robert", "Cindy", "Cindy", "Sarah", "Sarah"],
"Score": [84, 45, 67, 87, 88, 100, 76, 91]
}
#load data into a DataFrame object:
df = pd.DataFrame(data)
df
I'd like it so there's one row of John with whatever the mean of John is. Same with Robert, Cindy and Sarah.
Thanks!
Solution
# groupby and mean
df.groupby('Name', as_index=False)['Score'].mean()
Name Score
0 Cindy 94.0
1 John 64.5
2 Robert 77.0
3 Sarah 83.5
Answered By - Naveed
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.