pandas添加新列

一、读取数据

house_price = pd.read_csv('./data/house_price.csv')
print(house_price.head(3))

显示读取的csv文件的前三行,如下:

   Id  MSSubClass MSZoning  ...  SaleType  SaleCondition SalePrice
0   1          60       RL  ...        WD         Normal    208500
1   2          20       RL  ...        WD         Normal    181500
2   3          60       RL  ...        WD         Normal    223500

[3 rows x 81 columns]

二、添加新列

1、直接赋值法

利用pandas中的.loc方法,选中所有行,创建一个新的column

house_price.loc[:,'new'] = house_price['MSSubClass'] + house_price['LotFrontage']
print(house_price.head(3))

可以看到在最后一列创建了一个新的列标为“new”的新列

   Id  MSSubClass MSZoning  ...  SaleCondition  SalePrice    new
0   1          60       RL  ...         Normal     208500  125.0
1   2          20       RL  ...         Normal     181500  100.0
2   3          60       RL  ...         Normal     223500  128.0

[3 rows x 82 columns]

2、df.apply()方法

df.apply(),需要传入一个函数,根据函数的内容生成新的列,函数的参数默认传入所有的行,直接在原数据中更改,不生成新的Dataframe。axis=1表示沿第二个维度进行添加,也就是添加新列。

def get_new(x):
    return x['MSSubClass'] + x['LotFrontage']
house_price['new'] = house_price.apply(get_new,axis=1)
print(house_price.head(3))

由运行的结果可以看到,确实在最后一排创建了一个新列

   Id  MSSubClass MSZoning  ...  SaleCondition  SalePrice    new
0   1          60       RL  ...         Normal     208500  125.0
1   2          20       RL  ...         Normal     181500  100.0
2   3          60       RL  ...         Normal     223500  128.0

[3 rows x 82 columns]

3、df.assign()方法

df.assign()方法,可以同时添加多列,但是返回了一个新的对象,其使用方法如下代码

def get_new(x):
    return x['MSSubClass'] + x['LotFrontage']

def get_new_2(x):
    return x['MSSubClass'] - x['LotFrontage']

h1 = house_price.assign(
    new_1 = get_new,
    new_2 = get_new_2
)
print(h1.head(3))

由运行结果可知,df.assign()方法确实在结尾处新加了"new_1"和"new_2"两列

   Id  MSSubClass MSZoning  LotFrontage  ...  SalePrice    new  new_1 new_2
0   1          60       RL         65.0  ...     208500  125.0  125.0  -5.0
1   2          20       RL         80.0  ...     181500  100.0  100.0 -60.0
2   3          60       RL         68.0  ...     223500  128.0  128.0  -8.0

[3 rows x 84 columns]

4、条件直接赋值

# #广播机制,会将这一列每一个值都赋值为空字符串
house_price['new_3'] = ''
house_price.loc[house_price['Id'] < 3,'new_3'] = 'small'
house_price.loc[house_price['Id'] >= 3,'new_3'] = 'big'

print(house_price.head(5))

运行结果如下,也可以达到创建新列的目的

   Id  MSSubClass MSZoning  LotFrontage  ...  SaleCondition SalePrice    new  new_3
0   1          60       RL         65.0  ...         Normal    208500  125.0  small
1   2          20       RL         80.0  ...         Normal    181500  100.0  small
2   3          60       RL         68.0  ...         Normal    223500  128.0    big
3   4          70       RL         60.0  ...        Abnorml    140000  130.0    big
4   5          60       RL         84.0  ...         Normal    250000  144.0    big

[5 rows x 83 columns]

以上就是pandas中创建新列的4种方法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

transuperb

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值