Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Return multiple columns using Pandas apply() method

  • Last Updated : 05 Sep, 2020

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: DataFrame.apply(func, axis=0, broadcast=None, raw=False, reduce=None, result_type=None, args=(), **kwds)

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

Below are some programs which depicts the use of pandas.DataFrame.apply()



Example 1:




# Program to illustrate the use of 
# pandas.DataFrame.apply() method
  
# Importing required Libraries
import pandas
import numpy
  
# Creating dataframe
dataFrame = pandas.DataFrame([[4, 9], ] * 3, columns =['A', 'B'])
print('Data Frame:')
display(dataFrame)
  
# Using pandas.DataFrame.apply() on the data frame
print('Returning multiple columns from Pandas apply()')
dataFrame.apply(numpy.sqrt)

Output:

pandas-apply-1
Using a numpy universal function (in this case the same as numpy.sqrt(dataFrame)).

Example 2:




# Program to illustrate the use of 
# pandas.DataFrame.apply() method
  
# Importing required Libraries
import pandas
import numpy
  
# Creating dataframe
dataFrame = pandas.DataFrame([[4, 9], ] * 3, columns =['A', 'B'])
print('Data Frame:')
display(dataFrame)
  
# Using pandas.DataFrame.apply() on the data frame
print('Returning multiple columns from Pandas apply()')
dataFrame.apply(numpy.sum, axis = 0)

Output:

pandas-apply-2
Using a reducing function on columns.

Example 3:




# Program to illustrate the use of 
# pandas.DataFrame.apply() method
  
# Importing required Libraries
import pandas
import numpy
  
# Creating dataframe
dataFrame = pandas.DataFrame([[4, 9], ] * 3, columns =['A', 'B'])
print('Data Frame:')
display(dataFrame)
  
# Using pandas.DataFrame.apply() on the data frame
print('Returning multiple columns from Pandas apply()')
dataFrame.apply(numpy.sum, axis = 1)

Output:



pandas-apply-3

Using a reducing function on rows.

Example 4:




# Program to illustrate the use of 
# pandas.DataFrame.apply() method
  
# Importing required Libraries
import pandas
import numpy
  
# Creating dataframe
dataFrame = pandas.DataFrame([[4, 9], ] * 3, columns =['A', 'B'])
print('Data Frame:')
display(dataFrame)
  
# Using pandas.DataFrame.apply() on the data frame
print('Returning multiple columns from Pandas apply()')
dataFrame.apply(lambda x: [1, 2], axis = 1)

Output:

pandas-apply-4

Returning a list-like will result in a Series.

Example 5:




# Program to illustrate the use of 
# pandas.DataFrame.apply() method
  
# Importing required Libraries
import pandas
import numpy
  
# Creating dataframe
dataFrame = pandas.DataFrame([[4, 9], ] * 3, columns =['A', 'B'])
print('Data Frame:')
display(dataFrame)
  
# 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:

pandas-apply-5

Passing result_type=’expand’ will expand list-like results to columns of a Dataframe.



Example 6:




# Program to illustrate the use of 
# pandas.DataFrame.apply() method
  
# Importing required Libraries
import pandas
import numpy
  
# Creating dataframe
dataFrame = pandas.DataFrame([[4, 9], ] * 3, columns =['A', 'B'])
print('Data Frame:')
display(dataFrame)
  
# 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:

pandas-apply-6

Returning a Series inside the function is similar to passing result_type=’expand’. The resulting column names will be the Series index.

Example 7:




# Program to illustrate the use of 
# pandas.DataFrame.apply() method
  
# Importing required Libraries
import pandas
import numpy
  
# Creating dataframe
dataFrame = pandas.DataFrame([[4, 9], ] * 3, columns =['A', 'B'])
print('Data Frame:')
display(dataFrame)
  
# 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:

pandas-apply-7
Passing result_type=’broadcast’ will ensure the same shape result, whether list-like or scalar is returned by the function, and broadcast it along the axis. The resulting column names will be the originals.




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!