如何从Python中创建的数据框中删除索引? [英] How to remove index from a created Dataframe in Python?
本文介绍了如何从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!
推荐答案
您可以使用文档:
import pandas as pd
list1 = [1,2]
list2 = [2,5]
df=pd.DataFrame({'Name' : list1,'Probability' : list2})
print (df)
Name Probability
0 1 2
1 2 5
df.set_index('Name', inplace=True)
print (df)
Probability
Name
1 2
2 5
如果您还需要删除索引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屋!
查看全文