熊猫索引在我的示例中不返回行名

时间:2019-12-31 02:34:25

标签: python pandas

似乎 ([[String : Any]]?) $R18 = 1 value { [0] = 1 key/value pair { [0] = { key = "attached" value = { payload_data_0 = 0x0000600002d51b30 3 elements { [0] = 0x00006000036d2b80 3 key/value pairs [1] = 0x00006000036d32c0 3 key/value pairs [2] = 0x00006000036d2400 3 key/value pairs } payload_data_1 = 0x2f73746375646f72 payload_data_2 = 0x70692d6775626544 instance_type = 0x00007fd61984ab70 } } } 实际上正在执行df.index

例如:

df.RangeIndex

我可以确认行名是ok = pd.DataFrame({"game":["red","blue","green"], "player":["Tom","Brad","Dan"], "score":[100,300,250]}) ok.set_index(["game"]) ok.index 的值,但是game会产生以下内容:

df.index

看着RangeIndex(start=0, stop=3, step=1) 似乎认为这确实是一个help(ok.index)命令。那么如何获取行值?

1 个答案:

答案 0 :(得分:1)

ok = pd.DataFrame({"game":["red","blue","green"], "player":["Tom","Brad","Dan"], "score":[100,300,250]})
ok2 = ok.set_index(["game"])
ok2.index #which gives Index(['red', 'blue', 'green'], dtype='object', name='game')

如果您要更改原始DataFrame,则应在inplace=True方法中传递set_index

ok.set_index(["game"], inplace=True)

像上面的一样,将使DataFrame突变并根据需要设置索引