欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

【解决办法】read_csv()第一列作为index

发布时间:2025/4/16 编程问答 44 豆豆
生活随笔 收集整理的这篇文章主要介绍了 【解决办法】read_csv()第一列作为index 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

简述

在之前读取csv文件的时候,发现第一列被读到了dataframe的column中了,而不是读到了index中,查阅完文档后得到解决。

解决办法

  • index_col : int or sequence or False, default None
    * Column to use as the row labels of the DataFrame. If a sequence is given, a MultiIndex is used. If you have a malformed file with delimiters at the end of each line, you might consider index_col=False to force pandas to not use the first column as the index (row names)

比如说,下面这个代码就可以解决了~
将第一列变为index。这样的读取进来~

data = pd.read_csv('1.csv', index_col=0)

总结

以上是生活随笔为你收集整理的【解决办法】read_csv()第一列作为index的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。