Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
78 views
in Artificial Intelligence (AI) by (123k points)
Explain the use of the apply() function in Pandas.

Please log in or register to answer this question.

1 Answer

0 votes
by (123k points)

In Pandas, the apply() function is used to apply a function along the axis of a DataFrame or Series. It is a powerful tool for Data Science tasks as it allows you to perform custom operations on the data. You can use apply() with lambda functions or any custom function you define.

Example Code:

import pandas as pd

# Example 1: Applying a lambda function to a Series
data = pd.Series([10, 20, 30, 40])
squared_values = data.apply(lambda x: x**2)
print(squared_values)
# Output:
# 0    100
# 1    400
# 2    900
# 3   1600
# dtype: int64

# Example 2: Applying a custom function to a DataFrame
data = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
def sum_of_columns(row):
    return row['A'] + row['B']
data['Sum'] = data.apply(sum_of_columns, axis=1)
print(data)
# Output:
#    A  B  Sum
# 0  1  4    5
# 1  2  5    7
# 2  3  6    9
 

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...