The problem I have is that adding a row to DataFrame changes dtype of columns:
>>> from pandas import DataFrame
>>> df = DataFrame({'a' : range(10)}, dtype='i4')
>>> df
a
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
[10 rows x 1 columns]
I specifically specified dtype to be int32 (i.e., 'i4'), as can be seen:
>>> df.dtypes
a int32
dtype: object
However, adding a row changes dtype to float64:
>>> df.loc[10] = 99
>>> df
a
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 99
[11 rows x 1 columns]
>>> df.dtypes
a float64
dtype: object
I've tried specifying the dtype of the value that I add:
>>> import numpy as np
>>> df = DataFrame({'a' : np.arange(10, dtype=np.int32)})
>>> df.dtypes
a int32
dtype: object
>>> df.loc[10] = np.int32(0)
>>> df.dtypes
a float64
dtype: object
But that does not work either. Is there any solution, without using functions that return new objects?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…