相关疑难解决方法(0)

向pandas添加行DataFrame会更改dtype

我遇到的问题是向DataFrame添加一行会更改列的类型:

>>> 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]
Run Code Online (Sandbox Code Playgroud)

我特意指定dtype为int32(即'i4'),可以看出:

>>> df.dtypes
a    int32
dtype: object
Run Code Online (Sandbox Code Playgroud)

但是,添加行会将dtype更改为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 …
Run Code Online (Sandbox Code Playgroud)

python pandas

9
推荐指数
1
解决办法
2031
查看次数

标签 统计

pandas ×1

python ×1