gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 23:26:00 28 4
gpt4 key购买 nike

我正在尝试绘制 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com