Python:合并熊猫数据框只会在列

时间:2020-01-31 13:56:58

标签: python python-3.x pandas dataframe bigdata

我目前正在从事一个大数据项目,该项目需要将多个文件合并到一个可以通过SAS分析的表中。大部分工作已经完成,最终事实表需要添加到最终输出中。

尝试将事实表合并到最终输出时遇到了障碍。在已加载到其自己的数据框中的此csv文件中,存在以下列。

table name: POP
year | Borough | Population

在将要连接它们的数据集中,这些字段也与其他26个字段一起存在。首次尝试通过以下行合并时:

#Output = pd.merge(Output, POP, on=['year', 'Borough'], how='outer')

返回以下错误

ValueError:您正在尝试合并object和int64列。如果 您希望继续,应该使用pd.concat

我理解这只是数据类型不匹配,因此在merge命令之前添加了以下行:

POP['year'] = POP['year'].astype(object)

这样做可以允许程序“成功完成”执行,但是输出文件具有填充列,但是当应该为“年份”组合的每一行都具有适当的填充量时,会用NaN填充该文件。和在POP表中找到的“自治市镇”匹配项。

任何帮助将不胜感激,我在下面提供了更完整的代码摘录,供那些认为更易于解析的人使用:

import pandas as pd


#
# Add Population Data
#

#rename columns for easier joining
POP.rename(columns={"Area name":"Borough"}, inplace=True)
POP.rename(columns={"Persons":"Population"}, inplace=True)
POP.rename(columns={"Year":"year"}, inplace=True)

#convert type of output column to allow join
POP['year'] = POP['year'].astype(object)


#add to output file
Output = pd.merge(Output, POP, on=['year', 'Borough'], how='outer')

在下面还可以找到一些有关数据类型以及所涉及的项目和表格的形状的信息:

> Output table info
> 
> <class 'pandas.core.frame.DataFrame'> Int64Index: 34241 entries, 0 to
> 38179 Data columns (total 2 columns): year       34241 non-null object
> Borough    34241 non-null object dtypes: object(2) memory usage:
> 535.0+ KB None table shape: (34241, 36)
> ----------
> 
> POP table info <class 'pandas.core.frame.DataFrame'> RangeIndex: 357
> entries, 0 to 356 Data columns (total 3 columns): year          357
> non-null object Borough       357 non-null object Population    357
> non-null object dtypes: object(3) memory usage: 4.2+ KB None table
> shape: (357, 3)

最后,我很抱歉,如果有人问到或表达不正确,我是python的新手,这是我第一次使用stack作为贡献者

编辑:

(1)

此处要求的是数据样本:

这是输出数据框

[357 rows x 3 columns]
                  Borough       Date  totalIncidents  Calculated Mean Closure  \
0                  Camden 2013-11-06             2.0                    613.5   
1  Kensington and Chelsea 2013-11-06             NaN                      NaN   
2             Westminster 2013-11-06             1.0                    113.0   

   PM2.5 (ug/m3)  PM10 (ug/m3) (R)  SO2 (ug/m3) (R)  CO (mg m-3) (R)  \
0           9.55            16.200              5.3              NaN   
1          10.65            21.125              1.7              0.2   
2          19.90            30.600              NaN              0.7   

   NO (ug/m3) (R)  NO2 (ug/m3) (R)  O3 (ug/m3) (R)  Bus Stops  \
0      135.866670        82.033333            24.4      447.0   
1       80.360000        65.680000            29.3      270.0   
2      171.033333       109.000000            21.3      489.0   

   Cycle Parking Points  \
0                  67.0   
1                  27.0   
2                  45.0   

   Average Public Transport Access Index 2015 (AvPTAI2015)  \
0                                          24.316782         
1                                          23.262691         
2                                          39.750796         

  Public Transport Accessibility Levels (PTAL) Catagorisation  \
0                                                  5            
1                                                  5            
2                                                 6a            

   Underground Stations in Borough  \
0                             16.0   
1                             12.0   
2                             31.0   

  PM2.5 Daily Air Quality Index(DAQI) classification  \
0                                                Low   
1                                                Low   
2                                                Low   

   PM2.5 Above WHO 24hr mean Guidline  PM10 Above WHO 24hr mean Guidline  \
0                                 0.0                                0.0   
1                                 0.0                                0.0   
2                                 0.0                                0.0   

   O3 Above WHO 8hr mean Guidline*  NO2 Above WHO 1hr mean Guidline*  \
0                              1.0                               1.0   
1                              1.0                               1.0   
2                              1.0                               1.0   

   SO2 Above WHO 24hr mean Guidline  SO2 Above EU 24hr mean Allowence  \
0                               0.0                               0.0   
1                               0.0                               0.0   
2                               0.0                               0.0   

   NO2 Above EU 24hr mean Allowence  CO Above EU 8hr mean Allowence  \
0                               1.0                             0.0   
1                               1.0                             0.0   
2                               1.0                             0.0   

   O3 Above EU 8hr mean Allowence  year  NO2 Year Mean (ug/m3)  \
0                             0.0  2013              50.003618   
1                             0.0  2013              50.003618   
2                             0.0  2013              50.003618   

   PM2.5 Year Mean (ug/m3)  PM10 Year Mean (ug/m3)  \
0                15.339228               24.530299   
1                15.339228               24.530299   
2                15.339228               24.530299   

   NO2 Above WHO Annual mean Guidline  NO2 Above EU Annual mean Allowence  \
0                                 0.0                                 1.0   
1                                 0.0                                 1.0   
2                                 0.0                                 1.0   

   PM2.5 Above EU Annual mean Allowence  PM10 Above EU Annual mean Allowence  \
0                                   0.0                                  0.0   
1                                   0.0                                  0.0   
2                                   0.0                                  0.0   

  Number of Bicycle Hires (All Boroughs)  
0                                 18,431  
1                                 18,431  
2                                 18,431 

这是“人口”数据框

   year               Borough Population
0  2010  Barking and Dagenham   182,838 
1  2011  Barking and Dagenham   187,029 
2  2012  Barking and Dagenham   190,560 

编辑(2): 所以这似乎是一个日期类型的问题,但我仍然不确定为什么要重铸数据类型。但是,finall使我着迷的解决方案是将输出数据帧另存为csv,然后将其重新加载到程序中,从那里合并开始再次工作。

0 个答案:

没有答案