Python Forum
Why reindex converts integer to decimal?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why reindex converts integer to decimal?
#1
Hi, I have the following code:

frame = pd.DataFrame(np.arange(9).reshape((3,3)),
                     index=['a', 'c', 'd'],
                     columns=['Ohio', 'Texas', 'California'])
So the elements in the DataFrame are integers from 0 to 8 as indicated below:

Ohio Texas California
a 0 1 2
c 3 4 5
d 6 7 8

However, when I executed:
 frame2 = frame.reindex(['a', 'b', 'c', 'd']) 
, I got:

Ohio Texas California
a 0.0 1.0 2.0
b NaN NaN NaN
c 3.0 4.0 5.0
d 6.0 7.0 8.0


Q1: Why the elements became decimals?

Strangely, when I typed the following:

states = ['Texas', 'Utah', 'California']                                                                                                 
frame.reindex(columns=states)                                                                                                            
I got the following with the elements back to integer.

Texas Utah California
a 1 NaN 2
c 4 NaN 5
d 7 NaN 8


When I executed:

 frame.loc[['a', 'b', 'c', 'd'], states]      
Texas Utah California
a 1.0 NaN 2.0
b NaN NaN NaN
c 4.0 NaN 5.0
d 7.0 NaN 8.0

As shown above, the elements became decimals again.

How come? Could you please let me know what is going on?
Reply


Messages In This Thread
Why reindex converts integer to decimal? - by new_to_python - Feb-08-2020, 02:23 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  reindex() Truman 2 3,115 Jul-17-2020, 10:21 PM
Last Post: Truman
  reindex dataframe after sorting Clunk_Head 4 4,746 Jun-26-2019, 01:04 AM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020