使用pandas.apply()将一个函数应用到Dataframe的每一行或每一列

使用pandas.apply()将一个函数应用到Dataframe的每一行或每一列

有不同的方法将一个函数应用到DataFrame的每一行或每一列。我们将在这篇文章中了解各种方法。让我们先创建一个小的数据框架,看看。

# import pandas and numpy library
import pandas as pd
import numpy as np
  
# list of tuples
matrix = [(1,2,3,4),
          (5,6,7,8,),
          (9,10,11,12),
          (13,14,15,16)
         ]
  
# Create a Dataframe object
df = pd.DataFrame(matrix, columns = list('abcd'))
  
# Output
df

输出 :

使用pandas.apply()将一个函数应用到Dataframe的每一行或每一列。

方法1:对每一行/列应用lambda函数。
例子1:对于列

# import pandas and numpy library
import pandas as pd
import numpy as np
 
# list of tuples
matrix = [(1,2,3,4),
          (5,6,7,8,),
          (9,10,11,12),
          (13,14,15,16)
         ]
 
# Create a Dataframe object
df = pd.DataFrame(matrix, columns = list('abcd'))
 
# Applying a lambda function to each
# column which will add 10 to the value
new_df = df.apply(lambda x : x + 10)
 
# Output
new_df

输出 :

使用pandas.apply()将一个函数应用到Dataframe的每一行或每一列。

例子2:对于行

# import pandas and numpy library
import pandas as pd
import numpy as np
 
# list of tuples
matrix = [(1,2,3,4),
          (5,6,7,8,),
          (9,10,11,12),
          (13,14,15,16)
         ]
 
# Creating a Dataframe object
df = pd.DataFrame(matrix, columns = list('abcd'))
 
# Applying a lambda function to each
# row which will add 5 to the value
new_df = df.apply(lambda x: x + 5, axis = 1)
 
# Output
new_df

输出 :

使用pandas.apply()将一个函数应用到Dataframe的每一行或每一列。

方法2:将用户定义的函数应用于每一行/每一列
例子1:对于列

# function to returns x*x
def squareData(x):
    return x * x
 
# import pandas and numpy packages
import pandas as pd
import numpy as np
 
# list of tuples
matrix = [(1,2,3,4),
          (5,6,7,8,),
          (9,10,11,12),
          (13,14,15,16)
         ]
 
# Creating a Dataframe object
df = pd.DataFrame(matrix, columns = list('abcd'))
 
# Applying a user defined function to
# each column that will square the given
# value
new_df = df.apply(squareData)
 
# Output
new_df

输出 :

使用pandas.apply()将一个函数应用到Dataframe的每一行或每一列。

例子2:对于行

# function to returns x*X
def squareData(x):
    return x * x
 
# import pandas and numpy library
import pandas as pd
import numpy as np
 
# List of tuples
matrix = [(1,2,3,4),
          (5,6,7,8,),
          (9,10,11,12),
          (13,14,15,16)
         ]
 
# Creating a Dataframe object
df = pd.DataFrame(matrix, columns = list('abcd'))
 
# Applying a user defined function
# to each row that will square the given value
new_df = df.apply(squareData, axis = 1)
 
# Output
new_df

输出 :

使用pandas.apply()将一个函数应用到Dataframe的每一行或每一列。

在上面的例子中,我们看到一个用户定义的函数是如何应用到每一行和每一列的。我们也可以应用需要两个参数的用户定义函数。

例子1:对于列

# function to returns x+y
def addData(x, y):
    return x + y
 
# import pandas and numpy library
import pandas as pd
import numpy as np
 
# list of tuples
matrix = [(1,2,3,4),
          (5,6,7,8,),
          (9,10,11,12),
          (13,14,15,16)
         ]
 
# Creating a Dataframe object
df = pd.DataFrame(matrix, columns = list('abcd'))
 
# Applying a user defined function to each
# column which will add value in each
# column by given number
new_df = df.apply(addData, args = [1])
 
# Output
print(new_df)

输出:

使用pandas.apply()将一个函数应用到Dataframe的每一行或每一列。

例子2:对于行

# function to returns x+y
def addData(x, y):
    return x + y
 
# import pandas and numpy library
import pandas as pd
import numpy as np
 
# List of tuples
matrix = [(1,2,3,4),
          (5,6,7,8,),
          (9,10,11,12),
          (13,14,15,16)
         ]
 
# Creating a Dataframe object
df = pd.DataFrame(matrix, columns = list('abcd'))
 
# Applying a user defined function to each
# row which will add value in each row by
# given number
new_df = df.apply(addData, axis = 1,
                    args = [3])
 
# Output
new_df

输出 :

使用pandas.apply()将一个函数应用到Dataframe的每一行或每一列。

方法3:将numpy函数应用于每一行/每一列
例子1:对于列

# import pandas and numpy library
import pandas as pd
import numpy as np
 
# list of tuples
matrix = [(1,2,3,4),
          (5,6,7,8,),
          (9,10,11,12),
          (13,14,15,16)
         ]
 
# Creating a dataframe object
df = pd.DataFrame(matrix, columns = list('abcd'))
 
# Applying a numpy function to each
# column by squaring each value
new_df = df.apply(np.square)
 
# Output
new_df

输出 :

使用pandas.apply()将一个函数应用到Dataframe的每一行或每一列。

例子2:对于行

# import pandas and numpy library
import pandas as pd
import numpy as np
 
# List of tuples
matrix = [(1,2,3,4),
          (5,6,7,8,),
          (9,10,11,12),
          (13,14,15,16)
         ]
 
# Creating a dataframe object
df = pd.DataFrame(matrix, columns = list('abcd'))
 
# Apply a numpy function to each row
# to find square root of each value
new_df = df.apply(np.sqrt, axis = 1)
 
# Output
new_df

输出 :

使用pandas.apply()将一个函数应用到Dataframe的每一行或每一列。

方法4:对每一行/每一列应用还原函数
Reducing函数将把行或列作为系列,并返回与输入行/列相同大小的系列,或者它将返回一个单一的变量,这取决于我们使用的函数。

例子1:对于列

# import pandas and numpy library
import pandas as pd
import numpy as np
 
# List of tuples
matrix = [(1,2,3,4),
          (5,6,7,8,),
          (9,10,11,12),
          (13,14,15,16)
         ]
 
# Creating a Dataframe object
df = pd.DataFrame(matrix, columns = list('abcd'))
 
# Applying a numpy function to get the sum
# of all values in each column
new_df = df.apply(np.sum)
 
# Output
new_df

输出 :

使用pandas.apply()将一个函数应用到Dataframe的每一行或每一列。

例子2:对于行

# import pandas and numpy library
import pandas as pd
import numpy as np
 
# List of tuples
matrix = [(1,2,3,4),
          (5,6,7,8,),
          (9,10,11,12),
          (13,14,15,16)
         ]
 
# Creating a Dataframe object
df = pd.DataFrame(matrix, columns = list('abcd'))
 
# Applying a numpy function to get t
# he sum of all values in each row
new_df = df.apply(np.sum, axis = 1)
 
# Output
new_df

输出 :

使用pandas.apply()将一个函数应用到Dataframe的每一行或每一列。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程