Python|熊猫 dataframe.assign()
Python是一种用于进行数据分析的出色语言,主要是因为以数据为中心的Python包的奇妙生态系统。 Pandas就是其中之一,它使导入和分析数据变得更加容易。
Dataframe.assign()
方法将新列分配给 DataFrame,返回一个新对象(副本),其中新列添加到原始列。重新分配的现有列将被覆盖。
新分配列的长度必须与数据框中的行数匹配。
Syntax: DataFrame.assign(**kwargs)
Parameters:
kwargs : keywords are the column names. If the values are callable, they are computed on the DataFrame and assigned to the new columns. The callable must not change input DataFrame (though pandas don’t check it). If the values are not callable, (e.g. a Series, scalar, or array), they are simply assigned.
Returns: A new DataFrame with the new columns in addition to all the existing columns.
有关代码中使用的 CSV 文件的链接,请单击此处
示例 #1:分配一个名为Revised_Salary
的新列,其增量为原始 Salary 的 10%。
# importing pandas as pd
import pandas as pd
# Making data frame from the csv file
df = pd.read_csv("nba.csv")
# Printing the first 10 rows of
# the data frame for visualization
df[:10]
# increase the salary by 10 %
df.assign(Revised_Salary = lambda x: df['Salary']
+ df['Salary']/10)
输出:
示例 #2:一次分配多个列
# importing pandas as pd
import pandas as pd
# Making data frame from the csv file
df = pd.read_csv("nba.csv")
# First column ='New_Team', this column
# will append '_GO' at the end of each team name.
# Second column ='Revised_Salary' will increase
# the salary of all employees by 10 %
df.assign(New_team = lambda x: df['Team']+'_GO',
Revised_Salary = lambda x: df['Salary']
+ df['Salary'] / 10)
输出: