How to Compute Row-wise Mean in Pandas: Two Simple Methods
This article demonstrates how to calculate the average of list‑type marks for each student in a Pandas DataFrame using two straightforward approaches, complete with code snippets and visual results to help Python learners solve similar data‑processing tasks.
1. Introduction
Hello, I am Pipipi. A question about processing data with Pandas was posted in a Python group, and this article provides a solution.
2. Original Data
df = pd.DataFrame({
'student_id': ['S001', 'S002', 'S003'],
'marks': [[88,89,90],[78,81,60],[84,83,91]]
})
print(df)The DataFrame contains a column of list‑type marks for each student.
3. Expected Result
4. Method One
Using map with a lambda function:
df['dmean'] = df['marks'].map(lambda x: np.mean(x))
print(df)5. Method Two
Using map or apply directly with np.mean:
df['dmean'] = df['marks'].map(np.mean)
# or
df['dmean'] = df['marks'].apply(np.mean)
print(df)6. Conclusion
The article presented a Pandas data‑processing problem and offered two concrete solutions that compute the mean of each student's marks, enabling readers to apply these techniques to similar tasks.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
