Pandas 散点图:指数 Out-of-Bounds
Pandas Scatter Plot: Indices Out-of-Bounds
我有一个简单的 Pandas 数据框:
delta
、start_hour
、end_hour
都是numpy.int64
:
type(df.delta[0])
->numpy.int64
每当我尝试使用 Pandas 方法绘制散点图时,我都会得到 "IndexError: indices are out-of-bounds"。例如:
sc2 = df.plot.scatter(x=df.delta, y=df.start_hour)
产生:
IndexError
Traceback (most recent call last)
<ipython-input-118-4d521c29b97f> in <module>()
----> 1 sc2 = df.plot.scatter(x=df.delta, y=df.start_hour)
...
/mnt/xarfuse/uid-116535/[edit]/pandas/core/indexing.pyc in maybe_convert_indices(indices, n)
IndexError: indices are out-of-bounds
我也尝试过显式转换为 Numpy 数组,如 this post:
中所述
df_x = np.array(df['delta'].tolist())
df_y = np.array(df['start_hour'].tolist())
sc1 = df.plot.scatter(x=df_x, y=df_y)
产生同样的错误。
我确信我遗漏了一些非常简单的东西。感谢帮助!
当你将 df['delta'] 传递给 x
时,它会像 df[df['delta']]
which returns a key error : not in index
,所以你必须简单地传递分散方法的列名作为 x 和 y 值,即
sc2 = df.plot.scatter(x='delta', y='start_hour')
示例
df = pd.DataFrame({'delta':[162,9,9,38,691,58],'start_hour':[1,5,11,1,7,6],'last_hour':[3,5,11,2,19,7]})
sc2 = df.plot.scatter(x='delta', y='start_hour')
plt.show()
如果你想传递 numpy 数组,那么不要在 df 中搜索它。即直接使用 plt.scatter
例如
df_x = np.array(df['delta'].tolist())
df_y = np.array(df['start_hour'].tolist())
plt.scatter(x=df_x, y=df_y)
plt.show()
希望对你有帮助
我有一个简单的 Pandas 数据框:
delta
、start_hour
、end_hour
都是numpy.int64
:
type(df.delta[0])
->numpy.int64
每当我尝试使用 Pandas 方法绘制散点图时,我都会得到 "IndexError: indices are out-of-bounds"。例如:
sc2 = df.plot.scatter(x=df.delta, y=df.start_hour)
产生:
IndexError
Traceback (most recent call last)
<ipython-input-118-4d521c29b97f> in <module>()
----> 1 sc2 = df.plot.scatter(x=df.delta, y=df.start_hour)
...
/mnt/xarfuse/uid-116535/[edit]/pandas/core/indexing.pyc in maybe_convert_indices(indices, n)
IndexError: indices are out-of-bounds
我也尝试过显式转换为 Numpy 数组,如 this post:
中所述df_x = np.array(df['delta'].tolist())
df_y = np.array(df['start_hour'].tolist())
sc1 = df.plot.scatter(x=df_x, y=df_y)
产生同样的错误。
我确信我遗漏了一些非常简单的东西。感谢帮助!
当你将 df['delta'] 传递给 x
时,它会像 df[df['delta']]
which returns a key error : not in index
,所以你必须简单地传递分散方法的列名作为 x 和 y 值,即
sc2 = df.plot.scatter(x='delta', y='start_hour')
示例
df = pd.DataFrame({'delta':[162,9,9,38,691,58],'start_hour':[1,5,11,1,7,6],'last_hour':[3,5,11,2,19,7]})
sc2 = df.plot.scatter(x='delta', y='start_hour')
plt.show()
如果你想传递 numpy 数组,那么不要在 df 中搜索它。即直接使用 plt.scatter
例如
df_x = np.array(df['delta'].tolist())
df_y = np.array(df['start_hour'].tolist())
plt.scatter(x=df_x, y=df_y)
plt.show()
希望对你有帮助