读取csv时删除pandas中的索引列 [英] Removing index column in pandas when reading a csv

查看:81
本文介绍了读取csv时删除pandas中的索引列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码可以导入 CSV 文件.有 3 列,我想将其中的前两列设置为变量.当我将第二列设置为变量效率"时,索引列也被添加.如何去掉索引列?

I have the following code which imports a CSV file. There are 3 columns and I want to set the first two of them to variables. When I set the second column to the variable "efficiency" the index column is also tacked on. How can I get rid of the index column?

df = pd.DataFrame.from_csv('Efficiency_Data.csv', header=0, parse_dates=False)
energy = df.index
efficiency = df.Efficiency
print efficiency

我尝试使用

del df['index']

在我设置之后

energy = df.index

这是我在另一篇文章中发现的,但结果是KeyError: 'index'"

which I found in another post but that results in "KeyError: 'index' "

推荐答案

DataFramesSeries 总是有一个索引.尽管它显示在列旁边,但它不是列,这就是 del df['index'] 不起作用的原因.

DataFrames and Series always have an index. Although it displays alongside the column(s), it is not a column, which is why del df['index'] did not work.

如果你想用简单的序列号替换索引,使用df.reset_index().

If you want to replace the index with simple sequential numbers, use df.reset_index().

要了解索引为何存在以及它是如何使用的,请参见例如10 分钟到 Pandas.

To get a sense for why the index is there and how it is used, see e.g. 10 minutes to Pandas.

这篇关于读取csv时删除pandas中的索引列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆