
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Calculate Mean Product of DataFrame Values in Python
Input −
Assume, sample DataFrame is,
Id Age salary 0 1 27 40000 1 2 22 25000 2 3 25 40000 3 4 23 35000 4 5 24 30000 5 6 32 30000 6 7 30 50000 7 8 28 20000 8 9 29 32000 9 10 27 23000
Output −
Result for mean and product of given slicing rows are,
mean is Age 23.333333 salary 33333.333333 product is Age 12650 salary 35000000000000
Solution
To solve this, we will follow the below approaches.
Define a DataFrame
Create a function, slice second, third, and fourth rows of age and salary columns using iloc function and store it in result DataFrame.
df.iloc[1:4,1:]
Calculate the mean and product from the result DataFrame.
Example
Let us see the following implementation to get a better understanding.
import pandas as pd def find_mean_prod(): data = [[1,27,40000],[2,22,25000],[3,25,40000],[4,23,35000],[5,24,30000], [6,32,30000],[7,30,50000],[8,28,20000],[9,29,32000],[10,27,23000]] df = pd.DataFrame(data,columns=('Id','Age','salary')) print(df) print("slicing second,third and fourth rows of age and salary columns\n") result = df.iloc[1:4,1:] print("mean is\n", result.mean()) print("product is\n", result.prod()) find_mean_prod()
Output
Id Age salary 0 1 27 40000 1 2 22 25000 2 3 25 40000 3 4 23 35000 4 5 24 30000 5 6 32 30000 6 7 30 50000 7 8 28 20000 8 9 29 32000 9 10 27 23000 slicing second,third and fourth rows of age and salary columns mean is Age 23.333333 salary 33333.333333 product is Age 12650 salary 35000000000000
Advertisements