用pandas中的自定义索引替换数字索引

时间:2017-10-12 09:36:00

标签: python pandas

我有一个这样的数据框:

{returnNewDocument:true}

我想删除索引列并为索引设置ip:

> db.test.save({i: 0.0})
WriteResult({ "nInserted" : 1 })
> db.test.find()
{ "_id" : ObjectId("59df37406ecc1229670714fd"), "i" : 0 }
> db.test.findOneAndUpdate({ "_id" : ObjectId("59df37406ecc1229670714fd")}, {$inc: {i: 1.6} }, {returnNewDocument:true})
{ "_id" : ObjectId("59df37406ecc1229670714fd"), "i" : 1.6 }
> db.test.findOneAndUpdate({ "_id" : ObjectId("59df37406ecc1229670714fd")}, {$inc: {i: -1.6} }, {returnNewDocument:true})
{ "_id" : ObjectId("59df37406ecc1229670714fd"), "i" : 0 }
> db.test.find()
{ "_id" : ObjectId("59df37406ecc1229670714fd"), "i" : 0 }
>

reset_index()和df.index.name无效...

1 个答案:

答案 0 :(得分:1)

Set desired and drop current:

df.set_index('ip', drop=True)

As it was pointed out in one of the comments, to make changes inplace you can use either:

df.set_index('ip', drop=True, inplace=True)

or

df = df.set_index('ip', drop=True)
相关问题