
本文介绍的是Pandas库中一个非常有用的函数:assign。
在我们处理数据的时候,有时需要根据某个列进行计算得到一个新列,以便后续使用,相当于是根据已知列得到新的列,这个时候assign函数非常方便。下面通过实例来说明函数的的用法。
本文是Pandas文章连载系列的第21篇,主要分为3类:
基础部分:1-16篇,主要是介绍Pandas中基础和常用操作,比如数据创建、检索查询、排名排序、缺失值/重复值处理等常见的数据处理操作
进阶部分:第17篇开始讲解Pandas中的高级操作方法
对比SQL,学习Pandas:将SQL和Pandas的操作对比起来进行学习
assign函数的参数只有一个:DataFrame.assign(**kwargs)。
**kwargs: dict of {str: callable or Series}关于参数的几点说明:
最后,这个函数的返回值是一个新的DataFrame数据框,包含所有现有列和新生成的列
import pandas as pd
import numpy as np# 模拟数据
df = pd.DataFrame({
  "col1":[12, 16, 18],
  "col2":["xiaoming","peter", "mike"]})
df.dataframe tbody tr th:only-of-type { vertical-align: middle; } <pre><code>.dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; } </code></pre>
col1  | col2  | |
|---|---|---|
0  | 12  | xiaoming  | 
1  | 16  | peter  | 
2  | 18  | mike  | 
当值是可调用的,我们直接在数据框上进行计算:
# 方式1:数据框df上调用
# 使用数据框df的col1属性,生成col3
df.assign(col3=lambda x: x.col1 / 2 + 20).dataframe tbody tr th:only-of-type { vertical-align: middle; } <pre><code>.dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; } </code></pre>
col1  | col2  | col3  | |
|---|---|---|---|
0  | 12  | xiaoming  | 26.0  | 
1  | 16  | peter  | 28.0  | 
2  | 18  | mike  | 29.0  | 
我们可以查看原来的df,发现它是不变的
df  # 原数据框不变的.dataframe tbody tr th:only-of-type { vertical-align: middle; } <pre><code>.dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; } </code></pre>
col1  | col2  | |
|---|---|---|
0  | 12  | xiaoming  | 
1  | 16  | peter  | 
2  | 18  | mike  | 
操作字符串类型的数据:
df.assign(col3=df["col2"].str.upper())可以通过直接引用现有的Series或序列来实现相同的行为:
# 方式2:调用现有的Series来计算
df.assign(col4=df["col1"] * 3 / 4 + 25)df  # 原数据不变.dataframe tbody tr th:only-of-type { vertical-align: middle; } <pre><code>.dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; } </code></pre>
col1  | col2  | |
|---|---|---|
0  | 12  | xiaoming  | 
1  | 16  | peter  | 
2  | 18  | mike  | 
在Python3.6+中,我们可以在同一个赋值中创建多个列,并且其中一个列还可以依赖于同一个赋值中定义的另一列,也就是中间生成的新列可以直接使用:
df.assign(
    col5=lambda x: x["col1"] / 2 + 10,
    col6=lambda x: x["col5"] * 5,  # 在col6计算中直接使用col5
    col7=lambda x: x.col2.str.upper(),
    col8=lambda x: x.col7.str.title()  # col8中使用col7
)df   # 原数据不变.dataframe tbody tr th:only-of-type { vertical-align: middle; } <pre><code>.dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: left; } </code></pre>
col1  | col2  | |
|---|---|---|
0  | 12  | xiaoming  | 
1  | 16  | peter  | 
2  | 18  | mike  | 
如果我们重新分配的是一个现有的列,那么这个现有列的值将会被覆盖:
df.assign(col1=df["col1"] / 2)  # col1直接被覆盖.dataframe tbody tr th:only-of-type { vertical-align: middle; } <pre><code>.dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: left; } </code></pre>
col1  | col2  | |
|---|---|---|
0  | 6.0  | xiaoming  | 
1  | 8.0  | peter  | 
2  | 9.0  | mike  | 
我们在pandas中同样可以使用apply函数来实现
df  # 原数据.dataframe tbody tr th:only-of-type { vertical-align: middle; } <pre><code>.dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: left; } </code></pre>
col1  | col2  | |
|---|---|---|
0  | 12  | xiaoming  | 
1  | 16  | peter  | 
2  | 18  | mike  | 
生成一个副本,我们直接在副本上操作:
df1 = df.copy()  # 生成副本,直接在副本上操作
df2 = df.copy()
df1.dataframe tbody tr th:only-of-type { vertical-align: middle; } <pre><code>.dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: left; } </code></pre>
col1  | col2  | |
|---|---|---|
0  | 12  | xiaoming  | 
1  | 16  | peter  | 
2  | 18  | mike  | 
df1.assign(col3=lambda x: x.col1 / 2 + 20).dataframe tbody tr th:only-of-type { vertical-align: middle; } <pre><code>.dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: left; } </code></pre>
col1  | col2  | col3  | |
|---|---|---|---|
0  | 12  | xiaoming  | 26.0  | 
1  | 16  | peter  | 28.0  | 
2  | 18  | mike  | 29.0  | 
df1  # df1保持不变.dataframe tbody tr th:only-of-type { vertical-align: middle; } <pre><code>.dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: left; } </code></pre>
col1  | col2  | |
|---|---|---|
0  | 12  | xiaoming  | 
1  | 16  | peter  | 
2  | 18  | mike  | 
df1["col3"] = df1["col1"].apply(lambda x:x / 2 + 20)
df1  # df1已经发生了变化.dataframe tbody tr th:only-of-type { vertical-align: middle; } <pre><code>.dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: left; } </code></pre>
col1  | col2  | col3  | |
|---|---|---|---|
0  | 12  | xiaoming  | 26.0  | 
1  | 16  | peter  | 28.0  | 
2  | 18  | mike  | 29.0  | 
我们发现:通过assign函数的操作,原数据是不变的,但是通过apply操作的数据已经变化了
最后在模拟一份数据,计算每个人的BMI。
身体质量指数,是BMI指数,简称体质指数,是国际上常用的衡量人体胖瘦程度以及是否健康的一个标准。
其中:体重单位是kg,身高单位是m
df2 = pd.DataFrame({
    "name":["xiaoming","xiaohong","xiaosu"],
    "weight":[78,65,87],
    "height":[1.82,1.75,1.89]
})
df2.dataframe tbody tr th:only-of-type { vertical-align: middle; } <pre><code>.dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: left; } </code></pre>
name  | weight  | height  | |
|---|---|---|---|
0  | xiaoming  | 78  | 1.82  | 
1  | xiaohong  | 65  | 1.75  | 
2  | xiaosu  | 87  | 1.89  | 
# 使用assign函数实现
df2.assign(BMI=df2["weight"] / (df2["height"] ** 2))df2 # 不变.dataframe tbody tr th:only-of-type { vertical-align: middle; } <pre><code>.dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: left; } </code></pre>
name  | weight  | height  | |
|---|---|---|---|
0  | xiaoming  | 78  | 1.82  | 
1  | xiaohong  | 65  | 1.75  | 
2  | xiaosu  | 87  | 1.89  | 
df2["BMI"] = df2["weight"] / (df2["height"] ** 2)
df2  # df2生成了一个新的列:BMI通过上面的例子,我们发现: