Return multiple columns using Pandas apply() method - GeeksforGeeks

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)
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 Librariesimport pandasimport numpy  # Creating dataframedataFrame = pandas.DataFrame([[4, 9], ] * 3, columns =['A', 'B'])print('Data Frame:')display(dataFrame)  # Using pandas.DataFrame.apply() on the data frameprint('Returning multiple columns from Pandas apply()')dataFrame.apply(numpy.sqrt)
Output:
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 Librariesimport pandasimport numpy  # Creating dataframedataFrame = pandas.DataFrame([[4, 9], ] * 3, columns =['A', 'B'])print('Data Frame:')display(dataFrame)  # Using pandas.DataFrame.apply() on the data frameprint('Returning multiple columns from Pandas apply()')dataFrame.apply(numpy.sum, axis = 0)
Output:
Using a reducing function on columns.
Example 3:
# Program to illustrate the use of # pandas.DataFrame.apply() method  # Importing required Librariesimport pandasimport numpy  # Creating dataframedataFrame = pandas.DataFrame([[4, 9], ] * 3, columns =['A', 'B'])print('Data Frame:')display(dataFrame)  # Using pandas.DataFrame.apply() on the data frameprint('Returning multiple columns from Pandas apply()')dataFrame.apply(numpy.sum, axis = 1)
Output:
Using a reducing function on rows.
Example 4:
# Program to illustrate the use of # pandas.DataFrame.apply() method  # Importing required Librariesimport pandasimport numpy  # Creating dataframedataFrame = pandas.DataFrame([[4, 9], ] * 3, columns =['A', 'B'])print('Data Frame:')display(dataFrame)  # Using pandas.DataFrame.apply() on the data frameprint('Returning multiple columns from Pandas apply()')dataFrame.apply(lambda x: [1, 2], axis = 1)
Output:
Returning a list-like will result in a Series.
Example 5:
# Program to illustrate the use of # pandas.DataFrame.apply() method  # Importing required Librariesimport pandasimport numpy  # Creating dataframedataFrame = pandas.DataFrame([[4, 9], ] * 3, columns =['A', 'B'])print('Data Frame:')display(dataFrame)  # Using pandas.DataFrame.apply() on the data frameprint('Returning multiple columns from Pandas apply()')dataFrame.apply(lambda x: [1, 2], axis = 1, result_type ='expand')
Output:
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 Librariesimport pandasimport numpy  # Creating dataframedataFrame = pandas.DataFrame([[4, 9], ] * 3, columns =['A', 'B'])print('Data Frame:')display(dataFrame)  # Using pandas.DataFrame.apply() on the data frameprint('Returning multiple columns from Pandas apply()')dataFrame.apply(lambda x: pandas.Series(    [1, 2], index =['foo', 'bar']), axis = 1)
Output:
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 Librariesimport pandasimport numpy  # Creating dataframedataFrame = pandas.DataFrame([[4, 9], ] * 3, columns =['A', 'B'])print('Data Frame:')display(dataFrame)  # Using pandas.DataFrame.apply() on the data frameprint('Returning multiple columns from Pandas apply()')dataFrame.apply(lambda x: [1, 2], axis = 1, result_type ='broadcast')
Output:
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.
Python pandas-dataFrame
Python pandas-dataFrame-methods
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Read a file line by line in Python
Enumerate() in Python
How to Install PIP on Windows ?
Iterate over a list in Python
Different ways to create Pandas Dataframe
Create a Pandas DataFrame from Lists
Python program to convert a list to string
Reading and Writing to text files in Python
*args and **kwargs in Python