如何从Python中创建的数据框中删除索引? [英] How to remove index from a created Dataframe in Python?

查看:982
本文介绍了如何从Python中创建的数据框中删除索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过使用以下命令合并2个列表创建了一个数据框df:

I have created a Dataframe df by merging 2 lists using the following command:

import pandas as pd
df=pd.DataFrame({'Name' : list1,'Probability' : list2})

但是我想删除第一列(索引列),并使名为Name的列成为第一列.我尝试使用del df['index']index_col=0.但是他们没有用.我还检查了reset_index(),这不是我所需要的.我想从这样创建的数据框中完全删除整个索引列(如上所述).有人请帮忙!

But I'd like to remove the first column (The index column) and make the column called Name the first column. I tried using del df['index'] and index_col=0. But they didn't work. I also checked reset_index() and that is not what I need. I would like to completely remove the whole index column from a Dataframe that has been created like this (As mentioned above). Someone please help!

推荐答案

您可以使用如果您还需要删除索引name:

If you need also remove index name:

df.set_index('Name', inplace=True)
#pandas 0.18.0 and higher
df = df.rename_axis(None)
#pandas bellow 0.18.0
#df.index.name = None
print (df)
   Probability
1            2
2            5

这篇关于如何从Python中创建的数据框中删除索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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