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

Python 为什么pd.concat会将结果数据类型从int更改为float?,python,pandas,dataframe,concat,Python,Pandas,Dataframe,Concat,我有三个数据帧:timestamp(带有时间戳)、dataSun(带有日出和日落的时间戳)、dataData(带有不同的气候数据)。数据帧时间戳具有数据类型“int64” timestamp.head() 时间戳 0 1521681600000 1 1521681900000 2 1521682200000 3 1521682500000 4 1521682800000 数据帧dataSun还具有数据类型“int64” 带有气候数据的数据框具有数据类型“float64” 我想把这三个数据

我有三个数据帧:timestamp(带有时间戳)、dataSun(带有日出和日落的时间戳)、dataData(带有不同的气候数据)。数据帧
时间戳
具有数据类型
“int64”

timestamp.head()
时间戳
0  1521681600000
1  1521681900000
2  1521682200000
3  1521682500000
4 1521682800000

数据帧
dataSun
还具有数据类型
“int64”

带有气候数据的数据框具有数据类型“float64”

我想把这三个数据帧连接在一起

dataResult = pd.concat((timestamp, dataSun, dataData), axis = 1)
dataResult.head()
       timestamp       sunrise        sunset  temperature     pressure     
0  1521681600000  1.521696e+12  1.521741e+12     2.490000  1018.000000   
1  1521681900000  1.521696e+12  1.521741e+12     2.408333  1017.833333   
2  1521682200000  1.521696e+12  1.521741e+12     2.326667  1017.666667   
3  1521682500000  1.521696e+12  1.521741e+12     2.245000  1017.500000   
4  1521682800000  1.521696e+12  1.521741e+12     2.163333  1017.333333   
5  1521683100000  1.521696e+12  1.521741e+12     2.081667  1017.166667   

weatherMeasurements.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 7188 entries, 0 to 7187
Data columns (total 6 columns):
timestamp      7188 non-null int64
sunrise        7176 non-null float64
sunset         7176 non-null float64
temperature    7176 non-null float64
pressure       7176 non-null float64
humidity       7176 non-null float64
dtypes: float64(5), int64(1)
dataResult=pd.concat((时间戳,dataSun,dataData),axis=1)
dataResult.head()
时间戳日出日落温度压力
0 1521681600000 1.521696e+12 1.521741e+12 2.490000 1018.000000
1 1521681900000 1.521696e+12 1.521741e+12 2.408333 1017.833333
2 1521682200000 1.521696e+12 1.521741e+12 2.3266667 1017.666667
3 1521682500000 1.521696e+12 1.521741e+12 2.245000 1017.500000
4 1521682800000 1.521696e+12 1.521741e+12 2.1633333 1017.333333
5 1521683100000 1.521696e+12 1.521741e+12 2.081667 1017.166667
weathermeasures.info()
范围索引:7188个条目,0到7187
数据列(共6列):
时间戳7188非空int64
日出7176非空浮点64
日落7176非空浮点64
温度7176非零浮动64
压力7176非零浮动64
湿度7176非零浮动64
数据类型:float64(5)、int64(1)
为什么
pd.concat
更改了值的数据类型
DataSun
?我尝试了不同的方法来连接数据帧。例如,我在一个数据帧中只连接了
时间戳
数据帧
,然后将结果数据帧连接到
数据帧
。但结果是一样的。 如何连接三个数据帧并保护数据类型

因此-

timestamp      7188 non-null int64
sunrise        7176 non-null float64
...
timestamp
具有7188个非空值,而
sunrise
及以后的版本具有7176个非空值。不用说,有12个值不是空的。。。意思是他们是南斯

由于nan是
dtype=float
,因此该列中的每一个其他值都会自动向上转换为float,并且大的浮点数通常用科学记数法表示

这就是原因,但这并不能真正解决你的问题。你现在的选择是

  • 使用
    dropna
  • 使用
    fillna
  • (现在您可以将这些行向下转换为int。)

  • 或者,如果使用
    join='internal'
    执行
    pd.concat
    ,则不会引入NAN,并且会保留数据类型

    pd.concat((timestamp, dataSun, dataData), axis=1, join='inner')
    
           timestamp        sunrise         sunset  temperature     pressure  \    
    0  1521681600000  1521696105000  1521740761000     2.490000  1018.000000   
    1  1521681900000  1521696105000  1521740761000     2.408333  1017.833333   
    2  1521682200000  1521696105000  1521740761000     2.326667  1017.666667   
    3  1521682500000  1521696105000  1521740761000     2.245000  1017.500000   
    4  1521682800000  1521696105000  1521740761000     2.163333  1017.333333   
    
       humidity  
    0      99.0  
    1      99.0  
    2      99.0  
    3      99.0  
    4      99.0 
    

  • 使用选项3,对每个数据帧的索引执行内部联接。

    从pandas 1.0.0开始,我相信您还有另一个选项,即首先使用。这将数据帧列转换为支持pd.NA的数据类型,从而避免了在回答中讨论的NAN问题。

    您完全正确。我没有仔细检查代码。我已经纠正了它,它工作的权利。谢谢!
    timestamp      7188 non-null int64
    sunrise        7176 non-null float64
    ...
    
    pd.concat((timestamp, dataSun, dataData), axis=1, join='inner')
    
           timestamp        sunrise         sunset  temperature     pressure  \    
    0  1521681600000  1521696105000  1521740761000     2.490000  1018.000000   
    1  1521681900000  1521696105000  1521740761000     2.408333  1017.833333   
    2  1521682200000  1521696105000  1521740761000     2.326667  1017.666667   
    3  1521682500000  1521696105000  1521740761000     2.245000  1017.500000   
    4  1521682800000  1521696105000  1521740761000     2.163333  1017.333333   
    
       humidity  
    0      99.0  
    1      99.0  
    2      99.0  
    3      99.0  
    4      99.0