简体   繁体   中英

Change float index value to string index value in Pandas Series

I have pandas series set like this:

1    357
0    212
Name: target, dtype: int64

When I used this code:

data=data.reindex(index=['A','B'])

My result is :

A   NaN
B   NaN
Name: target, dtype: float64

But I try to achieve this:

A    357
B    212
Name: target, dtype: int64

So, what mistake did I make?

Use set_axis for setting index to a series.

data.set_axis(['A','B'],inplace=False)

A    357
B    212
Name: target, dtype: int64

As the documentation say :

If you do reindex a new object is produced unless the new index is equivalent to the current one. Default values in the new index that do not have corresponding records in the dataframe are assigned NaN.

您可以通过以下方式进行操作:

data.index  = ['A','B']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM