【发布时间】:2018-03-20 12:39:30
【问题描述】:
我正在尝试使用 Pandas 替换数据。 以下是我的目标 -
- 从
temperature和windspeed列中删除字母。32 F将被32替换,6 mph将被6替换。 - 将
temperature和windspeed中的所有负数转换为NaN。-99999将转换为NaN。 - 将
event中的所有0转换为No Event
以下是我的代码
import pandas as pd
import numpy as np
input_data = {'day': ['1/1/2017', '1/4/2017', '1/5/2017', '1/6/2017',
'1/7/2017', '1/8/2017', '1/9/2017', '1/10/2017', '1/11/2017'],
'temperature': ['32 F', -99999, 28, -99999, '32 C', '38 F', 35, 34, 40],
'windspeed': ['6 mph', 9, -99999, 7, -88888, -99999, '10 kmph', 8, 12],
'event': ['Rain', 'Sunny', 'Snow', 0, 'Rain', 'Sunny', 0, 'Cloudy', 'Sunny']
}
weather2 = pd.DataFrame(input_data)
new_weather = weather2.replace({"temperature":{-99999: np.NaN,
-88888: np.NaN,
'[A-Za-z]':''},
"windspeed":{-99999: np.NaN,
-88888: np.NaN,
'[A-Za-z]':''},
"event":{0:'No Event'}},regex=True)
输出如下——
仅更改正则表达式。如何将正则表达式与其他替换相结合?
【问题讨论】:
-
你能把输入数据的图片改成文字吗? Please don't post images of code (or links to them)
-
为输入数据添加代码文本
标签: python regex python-3.x pandas replace