- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Write a Python program to perform table-wise pipe function in a dataframe
Assume, you have a dataframe and the result for table-wise function is,
Table wise function: Id Mark 0 6.0 85.0 1 7.0 95.0 2 8.0 75.0 3 9.0 90.0 4 10.0 95.0
Solution
To solve this, we will follow the steps given below −
Define a dataframe
Create a user-defined function avg with two arguments and return the result as (a+b/2). It is defined below,
def avg(a,b): return (a+b/2)
Apply pipe() function to perform table-wise function inside first value as avg() and the second argument as 10 to calculate the avg of all the dataframe values.
df.pipe(avg,10)
Example
Let’s check the following code to get a better understanding −
import pandas as pd df = pd.DataFrame({'Id':[1,2,3,4,5],'Mark':[80,90,70,85,90]}) print("DataFrame is:\n",df) print("Table wise function:") def avg(a,b): return (a+b/2) print(df.pipe(avg,10))
Output
DataFrame is: Id Mark 0 1 80 1 2 90 2 3 70 3 4 85 4 5 90 Table wise function: Id Mark 0 6.0 85.0 1 7.0 95.0 2 8.0 75.0 3 9.0 90.0 4 10.0 95.0
- Related Articles
- Write a program in Python to perform average of rolling window size 3 calculation in a given dataframe
- Write a program in Python to perform flatten the records in a given dataframe by C and F order
- Write a Python program to reshape a given dataframe in different ways
- How to apply functions element-wise in a dataframe in Python?
- Write a program in Python to convert a given dataframe to a LaTex document
- Write a Python program to export a dataframe to an html file
- Write a program in Python to localize Asian timezone for a given dataframe
- Write a Python program to quantify the shape of a distribution in a dataframe
- Write a program in Python to remove first duplicate rows in a given dataframe
- Write a program in Python to print the ‘A’ grade students’ names from a DataFrame
- Write a program in Python to transpose the index and columns in a given DataFrame
- Write a program in Python to covert the datatype of a particular column in a dataframe
- Write a Python program to sort a given DataFrame by name column in descending order
- Write a program in Python to modify the diagonal of a given DataFrame by 1
- Write a Python program to trim the minimum and maximum threshold value in a dataframe

Advertisements