# 风向雷达图 wind = list(weather['风向']) weather = pd.read_csv('廊坊23年5月天气.csv', dtype={'级数': str}) # weather['级数']=pd.to_numeric(weather['级数']) weather['级数'] = weather['级数'].str.replace('级', '').astype(int) wind_speed = list(weather['级数']) wind = change_wind(wind) 报错list indices must be integers or slices, not str,帮我优化下
时间: 2023-08-05 22:01:53 浏览: 123
你可以尝试将 `wind` 和 `wind_speed` 转成整数类型,然后再进行操作。修改代码如下:
```python
weather = pd.read_csv('廊坊23年5月天气.csv', dtype={'级数': str})
weather['级数'] = weather['级数'].str.replace('级', '').astype(int)
wind_speed = list(weather['级数'])
wind = list(weather['风向'])
def change_wind(wind):
for i in range(len(wind)):
if wind[i] == '北风':
wind[i] = 0
elif wind[i] == '东北风':
wind[i] = 45
elif wind[i] == '东风':
wind[i] = 90
elif wind[i] == '东南风':
wind[i] = 135
elif wind[i] == '南风':
wind[i] = 180
elif wind[i] == '西南风':
wind[i] = 225
elif wind[i] == '西风':
wind[i] = 270
elif wind[i] == '西北风':
wind[i] = 315
return wind
wind = change_wind(wind)
```
这样就可以避免出现 `list indices must be integers or slices, not str` 的错误了。
阅读全文