Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Write a Python function which accepts DataFrame Age, Salary columns second, third and fourth rows as input and find the mean, product of values
This tutorial shows how to write a Python function that accepts a DataFrame and calculates the mean and product of specific rows from Age and Salary columns using iloc for row slicing.
Sample DataFrame
Let's start with a sample DataFrame containing employee data ?
import pandas as pd
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)
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
Function to Calculate Mean and Product
We'll create a function that slices the second, third, and fourth rows (index 1, 2, 3) from Age and Salary columns ?
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("Original DataFrame:")
print(df)
print("\nSlicing second, third and fourth rows of Age and Salary columns:")
# Select rows 1:4 (index 1,2,3) and columns 1: (Age, salary)
result = df.iloc[1:4, 1:]
print(result)
print("\nMean is:")
print(result.mean())
print("\nProduct is:")
print(result.prod())
find_mean_prod()
Original DataFrame: 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: Age salary 1 22 25000 2 25 40000 3 23 35000 Mean is: Age 23.333333 salary 33333.333333 dtype: float64 Product is: Age 12650 salary 35000000000000 dtype: int64
How It Works
The function uses df.iloc[1:4, 1:] for slicing:
-
1:4selects rows at index 1, 2, 3 (second, third, fourth rows) -
1:selects all columns starting from index 1 (Age and salary columns) -
mean()calculates the average value for each column -
prod()calculates the product of all values in each column
Conclusion
Use iloc for position-based DataFrame slicing, then apply mean() and prod() methods to calculate statistics on the selected data subset.
Advertisements
