Return multiple columns using Pandas apply() method
Last Updated :
08 Sep, 2022
Objects passed to the pandas.apply() are Series objects whose index is either the DataFrame’s index (axis=0) or the DataFrame’s columns (axis=1). By default (result_type=None), the final return type is inferred from the return type of the applied function. Otherwise, it depends on the result_type argument.
Syntax:
s.apply(func, convert_dtype=True, args=())
Creating Dataframe to return multiple columns using apply() method
Python3
# Importing required Libraries
import pandas
import numpy
# Creating dataframe
dataFrame = pandas.DataFrame
([[4, 9], ] * 3, columns =['A', 'B'])
display(dataFrame)
Output:
Below are some programs which depict the use of pandas.DataFrame.apply()
Example 1:
Using a Numpy universal function (in this case the same as numpy.sqrt()).
Python3
# Using pandas.DataFrame.apply() on the data frame
print('Returning multiple columns from Pandas apply()')
dataFrame.apply(numpy.sqrt)
Output:
Returning multiple columns from Pandas apply()
A B
0 2.0 3.0
1 2.0 3.0
2 2.0 3.0
Example 2:
Using a reducing function on columns.
Python3
# Using pandas.DataFrame.apply() on the data frame
print('Returning multiple columns from Pandas apply()')
dataFrame.apply(numpy.sum, axis = 0)
Output:
Returning multiple columns from Pandas apply()
A 12
B 27
dtype: int64
Example 3:
Using a reducing function on rows.
Python3
# Using pandas.DataFrame.apply() on the data frame
print('Returning multiple columns from Pandas apply()')
dataFrame.apply(numpy.sum, axis = 1)
Output:
Returning multiple columns from Pandas apply()
0 13
1 13
2 13
dtype: int64
Example 4:
Returning a list-like will result in a Series using the lambda function.
Python3
# Using pandas.DataFrame.apply() on the data frame
print('Returning multiple columns from Pandas apply()')
dataFrame.apply(lambda x: [1, 2], axis = 1)
Output:
Returning multiple columns from Pandas apply()
0 [1, 2]
1 [1, 2]
2 [1, 2]
dtype: object
Example 5:
Passing result_type=’expand’ will expand list-like results to columns of a Dataframe.
Python3
# Using pandas.DataFrame.apply() on the data frame
print('Returning multiple columns from Pandas apply()')
dataFrame.apply(lambda x: [1, 2], axis = 1, result_type ='expand')
Output:
Returning multiple columns from Pandas apply()
0 1
0 1 2
1 1 2
2 1 2
Example 6:
Returning a Series inside the function is similar to passing result_type='expand'. The resulting column names will be the Series index.
Python3
# Using pandas.DataFrame.apply() on the data frame
print('Returning multiple columns from Pandas apply()')
dataFrame.apply(lambda x: pandas.Series(
[1, 2], index =['foo', 'bar']), axis = 1)
Output:
Returning multiple columns from Pandas apply()
foo bar
0 1 2
1 1 2
2 1 2
Example 7:
Passing result_type='broadcast' will ensure the same shape result, whether list-like or scalar is returned by the function, and broadcasted along the axis. The resulting column names will be the originals.
Python3
# Using pandas.DataFrame.apply() on the data frame
print('Returning multiple columns from Pandas apply()')
dataFrame.apply(lambda x: [1, 2], axis = 1, result_type ='broadcast')
Output:
Returning multiple columns from Pandas apply()
A B
0 1 2
1 1 2
2 1 2
Similar Reads
Apply String Methods Across Multiple Columns in a Pandas DataFrame We are given a dataframe in Pandas with multiple columns, and we want to apply string methods to transform the data within these columns. In this article, we will explore three different approaches to applying string methods to multiple columns of a dataframe. Apply String Methods to Multiple Column
2 min read
How to Apply a function to multiple columns in Pandas? Pandas.apply allow the users to pass a function and apply it on every single value of the Pandas series. Syntax of pandas.DataFrame.apply Syntax : DataFrame.apply(parameters) Parameters :Â func : Function to apply to each column or row.axis : Axis along which the function is appliedraw : Determines
3 min read
How to select multiple columns in a pandas dataframe Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. In this article, we will discuss all the different ways of selecting multiple columns
5 min read
Add multiple columns to dataframe in Pandas In Pandas, we have the freedom to add columns in the data frame whenever needed. There are multiple ways to add columns to pandas dataframe. Add multiple columns to a DataFrame using ListsPython3 # importing pandas library import pandas as pd # creating and initializing a nested list students = [['
3 min read
Highlight Pandas DataFrame's specific columns using apply() Let us see how to highlight specific columns of a Pandas DataFrame. We can do this using the apply() function of the Styler class. Styler.apply() Syntax : Styler.apply(func, axis = 0, subset = None, **kwargs) Parameters : func : function should take a Series or DataFrame (depending on-axis), and ret
2 min read
Creating Pivot Table with Multiple Columns using Python Pandas PythonPandas make data manipulation, representation and analysis easier. Pandas Pivot Tables are used to create spreadsheet-style pivot tables as a DataFrame. The levels in the pivot table will be stored in MultiIndex objects (hierarchical indexes) on the index and columns of the result DataFrame. P
4 min read