将函数应用于 Pandas Dataframe 中的单个或选定的列或行
在本文中,我们将学习将函数应用于 Dataframe 中单个或选定的列或行的不同方法。我们将使用Dataframe/series.apply()方法来应用一个函数。
Syntax: Dataframe/series.apply(func, convert_dtype=True, args=())
Parameters: This method will take following parameters :
func: It takes a function and applies it to all values of pandas series.
convert_dtype: Convert dtype as per the function’s operation.
args=(): Additional arguments to pass to function instead of series.
Return Type: Pandas Series after applied function/operation.
方法一:使用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:使用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
输出 :
方法 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
输出 :
我们还可以将函数应用于数据框中的多个列或行。
示例 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:对于行。
# 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
输出 :
在评论中写代码?请使用 ide.geeksforgeeks.org,生成链接并在此处分享链接。