Pandas single row indexing without casting

2394 views python
6

Indexing a single row of a Pandas DataFrame containing objects (strings) returns a Series which is of type object.

df1 = pd.DataFrame({'a': np.arange(3), 'b': np.arange(0.5,3),'c': list('def')})

df1
   a    b  c
0  0  0.5  d
1  1  1.5  e
2  2  2.5  f

df1.loc[0]
a      0
b    0.5
c      d
Name: 0, dtype: object

Indexing a single row of a Pandas DataFrame not containing objects (strings) returns a Series which casts its values. Column a is of type int64 in the DataFrame. The resulting series contains the value of column a as a float64.

df2 = pd.DataFrame({'a': np.arange(3), 'b': np.arange(0.5,3)})

df2
  a    b
0  0  0.5
1  1  1.5
2  2  2.5

df2.loc[0]

a    0.0
b    0.5
Name: 0, dtype: float64

This behavior does not seem consistant, does it? How can I select a single row of the second DataFrame df2 without casting my ints to floats?

answered question

1 Answer

8

When there is more than one type in the Series , pandas will using object to handle it , If it is float and int , it will change the int to float

df1.loc[0].map(type)
Out[6]: 
a      <class 'numpy.int32'>
b    <class 'numpy.float64'>
c              <class 'str'>
Name: 0, dtype: object

posted this

Have an answer?

JD

Please login first before posting an answer.