python-3.x - pd.to_numeric 不工作
问题描述
我面临着一个奇怪的熊猫问题。
我不知道我哪里出错了?
但是当我创建一个新的df时,似乎没有问题。喜欢
知道为什么吗?
编辑 :
sat=pd.read_csv("2012_SAT_Results.csv")
sat.head()
#converted columns to numeric types
sat.iloc[:,2:]=sat.iloc[:,2:].apply(pd.to_numeric,errors="coerce")
sat.dtypes
sat_1=sat.iloc[:,2:].apply(pd.to_numeric,errors="coerce")
sat_1.head()
解决方案
您不能to_numeric
直接应用的事实.iloc
似乎是一个错误,但要获得您正在寻找的相同结果(同时应用于to_numeric
多个列),您可以改为使用:
df = pd.DataFrame({'a':['1','2'],'b':['3','4']})
# If you're applying to entire columns
df[df.columns[1:]] = df[df.columns[1:]].apply(pd.to_numeric, errors = 'coerce')
# If you want to apply to specific rows within columns
df.loc[df.index[1:], df.columns[1:]] = df.loc[df.index[1:], df.columns[1:]].apply(pd.to_numeric, errors = 'coerce')
推荐阅读
- java - Logstash 升级版本后无法运行实例
- c++ - Bash 脚本无法从 C++ 代码正常运行
- c - Makefile 过滤只是...不起作用?
- c# - 未处理的异常。无法归档或组装“系统内存”
- javascript - Chrome 扩展清单 v3 和内联脚本
- python - 尽管在python中显示十进制值,但显示唯一计数
- tkinter - 错误,在使用 iconbitmap tkinter.TclError: bitmap "image\inventory.ico" 未定义
- javascript - 如何在 React Grid 布局中为 WidthProvider 赋予特定的宽度值
- node.js - 每个 Node.js 发送令牌 CSRF Laravel - 错误 CSRF 令牌不匹配
- vb.net - 如何在 VB.NET 中专门化模板/泛型?