📌  相关文章
📜  将函数应用于 Pandas Dataframe 中的单个或选定的列或行

📅  最后修改于: 2022-05-13 01:55:47.016000             🧑  作者: Mango

将函数应用于 Pandas Dataframe 中的单个或选定的列或行

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

方法一:使用Dataframe.apply()lambda function
示例 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

输出 :
数据框

示例 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

输出 :
数据框-2

方法 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

输出 :
数据框

示例 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

输出 :
数据框-2

方法 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)

输出 :
数据框

示例 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

输出 :
数据框-2

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

示例 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

输出 :
数据框-2

示例 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

输出 :
数据框-1