python - 为什么我会得到 this cannot index with vector containing NA/NaN values 错误

标签 python database pandas matplotlib

我正在尝试绘制 pandas 数据框的两列。我以前用过 Pandas ,但已经有一段时间了。我试图通过简单地做来绘制:

df.plot.scatter(df['Time of Last Submission'], df['Last Attempt'])

我的数据框有几列,但我感兴趣的两列看起来像:

Last Attempt      Time of Last Submission  
15               2019-09-01 17:18:43.800202  
127              2019-09-01 17:18:43.822987  
148              2019-09-01 17:18:43.830904  
15               2019-09-01 17:18:43.853714  
NaN                         NaN  
134              2019-09-01 17:18:43.877629 

当我尝试像这样绘图时出现错误:

cannot index with vector containing NA / NaN values

我认为绘图时会忽略 NaN 值?任何帮助将不胜感激。

最佳答案

首先通过 to_datetime 将列转换为日期时间使用 errors='coerce' 将非日期时间字符串替换为缺失值 (NaT),然后使用 plt.scatter:

import matplotlib.pyplot as plt

df['Time of Last Submission'] = pd.to_datetime(df['Time of Last Submission'], errors='coerce')
plt.scatter(df['Time of Last Submission'], df['Last Attempt'])

#ImportanceOfBeingErnest solution from comments
#plt.scatter('Time of Last Submission', 'Last Attempt', data=df) 

因为:

df.plot.scatter('Time of Last Submission', 'Last Attempt')

ValueError: scatter requires x column to be numeric

关于python - 为什么我会得到 this cannot index with vector containing NA/NaN values 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57758114/

相关文章:

不能从 class.__dict__ 调用 python 类方法

sql - 从不同列中检索数据序列

java - 在 Spring 类中获取 Hibernate 事务

python - 将数据转换为分位数 bin

python - 如何修改 Pandas DataFrame 并插入新列

python - 获得列中 n 最小总和的组

python - 还有其他方法可以使用 python 设置 dict 值吗?

python - 导入的工作原理。为什么导入的模块不继承其他导入的模块

python - 如果使用python在捕获区域中存在某些颜色,如何返回 bool 值

python - 如何使用带有 pyodbc 的 cursor.fetchall 从数据库中的行创建列表?