按字典替换表格中的内容

1.原数据
在这里插入图片描述

from collections import *
weather = pd.read_csv('dataset/train_data/city_A/weather.csv', 
            header=None,
            names=['data', 'hour', 'temperature', 'humidity','wind_direction', 'wind_speed', 'wind_force','weather'])
weather=weather.dropna(axis=0,how='any') #drop all rows that have any NaN values
##以下为主要代码
#替换的字典
wind_direction_dict = {'North': 0, 'Northwest': 1, 'East': 2, 'Northeast': 3, 'South': 4, 'Southeast': 5, 'Southwest': 6, 'West': 7, 'Quiet': 8}
wind_speed_dict = {'<12km/h': 0, '16-24km/h': 1, '24-34km/h': 2}
weather_dict = {'Sunny': 0, 'Cloudy': 1, 'Overcast': 2, 'Rain': 3, 'Fog': 4}
#处理过程
weather=weather.replace(wind_direction_dict) 
weather=weather.replace(wind_speed_dict) 
weather.replace(weather_dict) 

处理之后就变这样了,在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值