python删除列索引,如何从Python中创建的数据框中删除索引?

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})

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!

解决方案

You can use set_index, docs:

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

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值