在Pandas数据框架中对单一或选定的列或行应用一个函数

在Pandas数据框架中对单一或选定的列或行应用一个函数

在这篇文章中,我们将学习在Dataframe中对单个或选定的列或行应用一个函数的不同方法。我们将使用Dataframe/series.apply()方法来应用一个函数。

语法: Dataframe/series.apply(func, convert_dtype=True, args=())

参数:此方法将接受以下参数。
func:它接收一个函数并将其应用于pandas系列的所有值。
convert_dtype:按照函数的操作转换dtype。
args=():额外的参数来代替系列传递给函数。

返回类型:应用函数/操作后的Pandas系列。

方法1:使用Dataframe.apply()和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)
         ]
  
# Create a DataFrame object
df = pd.DataFrame(matrix, columns = list('xyz'), 
                  index = list('abc'))
  
# Apply function numpy.square() to lambda
# to find the squares of the values of 
# column whose column name is 'z'
new_df = df.apply(lambda x: np.square(x) if x.name == 'z' else x)
  
# Output
new_df

输出 :
在Pandas数据框架中对单一或选定的列或行应用一个函数

例子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)
         ]
  
# Create a DataFrame object
df = pd.DataFrame(matrix, columns = list('xyz'), 
                   index = list('abc'))
  
# Apply function numpy.square() to lambda 
# to find the squares of the values of row
# whose row index is 'b'
new_df = df.apply(lambda x: np.square(x) if x.name == 'b' else x, 
                axis = 1)
  
# Output
new_df

输出 :
在Pandas数据框架中对单一或选定的列或行应用一个函数

方法2:使用Dataframe/series.apply() & [ ] 操作器。

例子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)
         ]
  
# Create a DataFrame object
df = pd.DataFrame(matrix, columns = list('xyz'), 
                   index = list('abc'))
  
# Apply a function to one column 'z'
# and assign it back to the same column 
df['z'] = df['z'].apply(np.square)
  
# Output
df

输出 :
在Pandas数据框架中对单一或选定的列或行应用一个函数

例子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)
         ]
  
# Create a DataFrame object
df = pd.DataFrame(matrix, columns = list('xyz'), 
                  index = list('abc'))
  
# Apply a function to one row 'b' 
# and assign it back to the same row 
df.loc['b'] = df.loc['b'].apply(np.square)
  
# Output
df

输出 :
在Pandas数据框架中对单一或选定的列或行应用一个函数

方法3:使用numpy.square()方法和[ ]操作符。
例子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)
         ]
  
# Create a DataFrame object
df = pd.DataFrame(matrix, columns = list('xyz'), 
                  index = list('abc'))
  
# Apply a function to one column 'z' and 
# assign it back to the same column 
df['z'] = np.square(df['z'])
  
# Output
print(df)

输出 :
在Pandas数据框架中对单一或选定的列或行应用一个函数

例子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)
         ]
  
# Create a DataFrame object
df = pd.DataFrame(matrix, columns = list('xyz'), index = list('abc'))
  
# Apply a function to one row 'b' and 
# assign it back to the same row
df.loc['b'] = np.square(df.loc['b'])
  
# Output
df

输出 :
在Pandas数据框架中对单一或选定的列或行应用一个函数

我们还可以将一个函数应用于数据框架中的多列或多行。

例子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)
         ]
  
# Create a DataFrame object
df = pd.DataFrame(matrix, columns = list('xyz'), 
                  index = list('abc'))
  
# Apply function numpy.square() 
# for square the values of
# two columns 'x' and 'y' 
new_df = df.apply(lambda x: np.square(x) if x.name in ['x', 'y'] else x)
  
# Output
new_df

输出 :
在Pandas数据框架中对单一或选定的列或行应用一个函数

例子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)
         ]
  
# Create a DataFrame object
df = pd.DataFrame(matrix, columns = list('xyz'),
                  index = list('abc'))
  
# Apply function numpy.square() to 
# square the values of two rows 
# 'b' and 'c'
new_df = df.apply(lambda x: np.square(x) if x.name in ['b', 'c'] else x,
                 axis = 1)
  
# Output
new_df

输出 :
在Pandas数据框架中对单一或选定的列或行应用一个函数

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程