Python 为什么pd.concat会将结果类型从int更改为object?

Python 为什么pd.concat会将结果类型从int更改为object?,python,pandas,dataframe,types,concat,Python,Pandas,Dataframe,Types,Concat,我正在用Pandas解析几个csv文件,并将它们连接到一个大数据帧中。然后,我想groupby并计算mean() 下面是一个示例数据帧: df1.head() df1.info(verbose=True) df_total.info(verbose=True)导致 <class 'pandas.core.frame.DataFrame'> Int64Index: 83916 entries, 0 to 55942 Data columns (total 3 columns): Tim

我正在用Pandas解析几个csv文件,并将它们连接到一个大数据帧中。然后,我想
groupby
并计算
mean()

下面是一个示例数据帧:

df1.head()

df1.info(verbose=True)

df_total.info(verbose=True)
导致

<class 'pandas.core.frame.DataFrame'>
Int64Index: 83916 entries, 0 to 55942
Data columns (total 3 columns):
Time       83916 non-null object
Node       83916 non-null object
Packets    83916 non-null object
dtypes: object(3)
memory usage: 2.6+ MB
None
这就是错误
pandas.core.base.DataError:没有要聚合的数值类型出现的地方

虽然我从其他帖子中了解到,Pandas因为
非空
而更改了
数据类型
,但我无法用建议的解决方案解决我的问题

我该如何解决这个问题

 df_total.info(verbose=True)
您的this语句以对象的形式提供信息,因此在连接时存在问题,每个值都不是int,因此对象的平均值不可能。

我发现另一个语句提到数据帧必须用数据类型初始化,否则它们是object类型

Did you initialize an empty DataFrame first and then filled it? If so that's probably
why it changed with the new version as before 0.9 empty DataFrames were initialized 
to float type but now they are of object type. If so you can change the 
initialization to DataFrame(dtype=float).

所以我在我的代码中添加了
df_-total=pd.DataFrame(columns=['Time','Node','Packets',dtype=int)
,它起了作用。

你可能想尝试
returned_-errors=[]
df_-total.groupby(['Time'])['Packets']:
尝试:
results.append([group[0],group[1].mean()])
除了:
returned_-error.append之外(组)
这将为您提供没有错误的任何组(如果有)的结果,并告诉您是哪些组导致了错误。
<class 'pandas.core.frame.DataFrame'>
Int64Index: 83916 entries, 0 to 55942
Data columns (total 3 columns):
Time       83916 non-null object
Node       83916 non-null object
Packets    83916 non-null object
dtypes: object(3)
memory usage: 2.6+ MB
None
df_total = df_total.groupby(['Time'])['Packets'].mean()
 df_total.info(verbose=True)
Did you initialize an empty DataFrame first and then filled it? If so that's probably
why it changed with the new version as before 0.9 empty DataFrames were initialized 
to float type but now they are of object type. If so you can change the 
initialization to DataFrame(dtype=float).