更改浮点索引值的字符串索引值熊猫系列
我有熊猫系列设置是这样的:更改浮点索引值的字符串索引值熊猫系列
1 357 0 212
Name: target, dtype: int64
当我用这个代码:
data=data.reindex(index=['A','B'])
我的结果是:
A NaN B NaN
Name: target, dtype: float64
但我试图做到这一点:
A 357 B 212
Name: target, dtype: int64
那么,我犯了什么错误?
回答:
使用set_axis
将索引设置为一系列。
data.set_axis(['A','B'],inplace=False) A 357
B 212
Name: target, dtype: int64
如文档说:
如果重新编制一个新的对象产生,除非新的索引是 相当于现在的一个。在 没有在数据帧中的相应记录的新索引中的默认值被分配为NaN。
回答:
你可以这样做:
data.index = ['A','B']
以上是 更改浮点索引值的字符串索引值熊猫系列 的全部内容, 来源链接: utcz.com/qa/265466.html