Feb-08-2020, 02:23 AM
(This post was last modified: Feb-08-2020, 02:23 AM by new_to_python.)
Hi, I have the following code:
Ohio Texas California
a 0 1 2
c 3 4 5
d 6 7 8
However, when I executed:
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:
Texas Utah California
a 1 NaN 2
c 4 NaN 5
d 7 NaN 8
When I executed:
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?
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?