使用 Pandas apply() 方法返回多列
传递给 pandas.apply() 的对象是 Series 对象,其索引是 DataFrame 的索引 (axis=0) 或 DataFrame 的列 (axis=1)。默认情况下(result_type=None),最终返回类型是从应用函数的返回类型推断出来的。否则,它取决于 result_type 参数。
Syntax: DataFrame.apply(func, axis=0, broadcast=None, raw=False, reduce=None, result_type=None, args=(), **kwds)
下面是一些描述pandas.DataFrame.apply()
使用的程序
示例 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)
输出:
使用 numpy 通用函数(在这种情况下与 numpy.sqrt(dataFrame) 相同)。
示例 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)
输出:
在列上使用归约函数。
示例 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)
输出:
对行使用归约函数。
示例 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)
输出:
返回一个类似列表将导致一个系列。
示例 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')
输出:
传递 result_type='expand' 会将类似列表的结果扩展到 Dataframe 的列。
示例 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)
输出:
在函数中返回 Series 类似于传递 result_type='expand'。生成的列名称将是系列索引。
示例 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')
输出:
传递 result_type='broadcast' 将确保函数返回相同的形状结果,无论是类列表还是标量,并沿轴广播。生成的列名称将是原始名称。