Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 附加到数据帧可转换数据类型_Python_Pandas - Fatal编程技术网

Python 附加到数据帧可转换数据类型

Python 附加到数据帧可转换数据类型,python,pandas,Python,Pandas,我正在追加pandas.DataFrame,列的数据类型以意外的方式转换: import pandas as pd df=pd.DataFrame({'a':1.0, 'b':'x'}, index=[0]) print df.dtypes df = df.append({'a':3.0}, ignore_index=True) print df.dtypes df = df.append({'a':3.0, 'b':'x'}, ignore_index=True) print df.dtype

我正在追加pandas.DataFrame,列的数据类型以意外的方式转换:

import pandas as pd
df=pd.DataFrame({'a':1.0, 'b':'x'}, index=[0])
print df.dtypes
df = df.append({'a':3.0}, ignore_index=True)
print df.dtypes
df = df.append({'a':3.0, 'b':'x'}, ignore_index=True)
print df.dtypes
输出:

a    float64
b     object
dtype: object
a    float64
b     object
dtype: object
a    object         <- ???
b    object
dtype: object
a浮点数64
b对象
数据类型:对象
花车
b对象
数据类型:对象

对象尝试此操作,首先将dict对象转换为数据帧:

import pandas as pd
df=pd.DataFrame({'a':1.0, 'b':'x'}, index=[0])
print df.dtypes
df = df.append({'a':3.0}, ignore_index=True)
print df.dtypes
df = df.append(pd.DataFrame([{'a':3.0, 'b':'x'}]), ignore_index=True)
print df.dtypes
或者,一个目录列表:

df = df.append([{'a':3.0, 'b':'x'}], ignore_index=True)
如果是dict,则首先将其转换为序列,序列包含3.0且“x”必须具有对象dtype


如果它是一个dict列表,它将被转换为一个数据帧,数据帧可以为每个列具有不同的数据类型。

Hmm。。奇怪的是,如果我这样做,我会得到
float
:df['a']中x的
:打印(type(x))
outputs:`对于每个项目,这会按预期报告
dtypes
float64
,你知道为什么带有dict的
append
不能按预期工作吗?这在master/0.13.1中已修复(当创建要附加的帧时,它没有转换对象数据类型,这是dict开始时的形式)。请参见此处: