
下图是我的列表,我想在其中编辑两个列以供将来在数据清理过程中进行分析:
运行代码 bike_share_data["start_lng"].dtypes 时,“start_lng”和“end_lng”列的内容为 dtype('o')
现在我想用减号(-)替换下划线(_)并使整个列的数据类型为浮点数。
我已经单独测试了代码,如下所示:
import pandas as pd
d =[ '_1.0', '_2.0', '_3.0']
d=[s.replace('_','-') for s in d]
print(d)结果是['-1.0', '-2.0', '-3.0']。
但无法在 bike_share_data["start_lng"] 列上实现它。我该怎么做?
您可以使用 str.replace() 方法执行替换,然后使用 astype() 更改数据类型。
# sample DataFrame with a "start_lng" column containing strings
data = {'start_lng': ['_1.0', '_2.0', '_3.0']}
Bike_share_data = pd.DataFrame(data)
# Replace underscores with minus signs & convert the column to float
Bike_share_data["start_lng"] = Bike_share_data["start_lng"].str.replace('_', '-').astype(float)以上就是如何在整列中用减号替换下划线?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号