df.set_index =>索引数据必须是一维的
我已经从 pandas 成功创建了一个 DataFrame df
。 但是,df.set_index('Time')
会抛出 ValueError: Index data must be 1-Dimensional
并且无法继续进行。
df=pd.DataFrame(lst, columns=['Time dtime Open Close High Low Volume'.split()])
df = df.astype({'Time':'datetime64[ms]', 'Open':float, 'High':float, 'Low':float, 'Close':float, 'Volume':float})
df.set_index('Time')
<but Error occurred...>
ValueError: Index data must be 1-dimensional
<print(df['时间'])
>
Time
0 1646498160000
1 1646498220000
2 1646498280000
3 1646498340000
4 1646498400000
怎么了?为什么“set_indexs”会抛出错误?
#================================================ #我其他模块中的代码示例做得很好
df = pd.DataFrame(d, columns='Time Open High Low Close'.split())
df = df.astype({'Time':'datetime64[ms]', 'Open':float, 'High':float, 'Low':float, 'Close':float})
df.set_index('Time')
I have successfully created a DataFrame df
from pandas.
However, df.set_index('Time')
throws ValueError: Index data must be 1-dimensional
and cannot proceed further.
df=pd.DataFrame(lst, columns=['Time dtime Open Close High Low Volume'.split()])
df = df.astype({'Time':'datetime64[ms]', 'Open':float, 'High':float, 'Low':float, 'Close':float, 'Volume':float})
df.set_index('Time')
<but Error occurred...>
ValueError: Index data must be 1-dimensional
<print(df['Time'])
>
Time
0 1646498160000
1 1646498220000
2 1646498280000
3 1646498340000
4 1646498400000
what's wrong? Why does 'set_indexs' throw an error?
#==============================================
#well done code example in my other modules
df = pd.DataFrame(d, columns='Time Open High Low Close'.split())
df = df.astype({'Time':'datetime64[ms]', 'Open':float, 'High':float, 'Low':float, 'Close':float})
df.set_index('Time')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第一行错误,需要删除
[]
以避免MultiIndex
:Wrong is first line, need remove
[]
for avoidMultiIndex
: