Thursday, December 26, 2024
Google search engine
HomeLanguagesReturn multiple columns using Pandas apply() method

Return multiple columns using Pandas apply() method

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 

RELATED ARTICLES

Most Popular

Recent Comments