美文网首页
Python之pandas表格合并

Python之pandas表格合并

作者: Brendansmisle | 来源:发表于2020-03-30 14:23 被阅读0次
    1.导入模块
    >>> import pandas as pd 
    >>> pd.set_option('display.max_rows', 100,'display.max_columns', 1000,"display.max_colwidth",1000,'display.width',1000)    #设置显示
    
    2.导入数据
    >>> air_quality_no2 = pd.read_csv(r"C:\Users\Administrator\Desktop\air_quality_no2_long.csv", parse_dates=True)
    >>> air_quality_no2 = air_quality_no2[["date.utc", "location", "parameter", "value"]]
    >>> air_quality_no2
                           date.utc            location parameter  value
    0     2019-06-21 00:00:00+00:00             FR04014       no2   20.0
    1     2019-06-20 23:00:00+00:00             FR04014       no2   21.8
    2     2019-06-20 22:00:00+00:00             FR04014       no2   26.5
    3     2019-06-20 21:00:00+00:00             FR04014       no2   24.9
    4     2019-06-20 20:00:00+00:00             FR04014       no2   21.4
    ...                         ...                 ...       ...    ...
    2063  2019-05-07 06:00:00+00:00  London Westminster       no2   26.0
    2064  2019-05-07 04:00:00+00:00  London Westminster       no2   16.0
    2065  2019-05-07 03:00:00+00:00  London Westminster       no2   19.0
    2066  2019-05-07 02:00:00+00:00  London Westminster       no2   19.0
    2067  2019-05-07 01:00:00+00:00  London Westminster       no2   23.0
    
    [2068 rows x 4 columns]
    >>> 
    >>> air_quality_pm25 = pd.read_csv(r"C:\Users\Administrator\Desktop\air_quality_pm25_long.csv", parse_dates=True)
    >>> air_quality_pm25 = air_quality_pm25[["date.utc", "location", "parameter", "value"]]
    >>> air_quality_pm25
                           date.utc            location parameter  value
    0     2019-06-18 06:00:00+00:00             BETR801      pm25   18.0
    1     2019-06-17 08:00:00+00:00             BETR801      pm25    6.5
    2     2019-06-17 07:00:00+00:00             BETR801      pm25   18.5
    3     2019-06-17 06:00:00+00:00             BETR801      pm25   16.0
    4     2019-06-17 05:00:00+00:00             BETR801      pm25    7.5
    ...                         ...                 ...       ...    ...
    1105  2019-05-07 06:00:00+00:00  London Westminster      pm25    9.0
    1106  2019-05-07 04:00:00+00:00  London Westminster      pm25    8.0
    1107  2019-05-07 03:00:00+00:00  London Westminster      pm25    8.0
    1108  2019-05-07 02:00:00+00:00  London Westminster      pm25    8.0
    1109  2019-05-07 01:00:00+00:00  London Westminster      pm25    8.0
    
    [1110 rows x 4 columns]
    
    3.合并数据,上下拼接
    合并方式
    >>> air_quality = pd.concat([air_quality_pm25, air_quality_no2], axis=0)
    >>> air_quality
                           date.utc            location parameter  value
    0     2019-06-18 06:00:00+00:00             BETR801      pm25   18.0
    1     2019-06-17 08:00:00+00:00             BETR801      pm25    6.5
    2     2019-06-17 07:00:00+00:00             BETR801      pm25   18.5
    3     2019-06-17 06:00:00+00:00             BETR801      pm25   16.0
    4     2019-06-17 05:00:00+00:00             BETR801      pm25    7.5
    ...                         ...                 ...       ...    ...
    2063  2019-05-07 06:00:00+00:00  London Westminster       no2   26.0
    2064  2019-05-07 04:00:00+00:00  London Westminster       no2   16.0
    2065  2019-05-07 03:00:00+00:00  London Westminster       no2   19.0
    2066  2019-05-07 02:00:00+00:00  London Westminster       no2   19.0
    2067  2019-05-07 01:00:00+00:00  London Westminster       no2   23.0
    
    [3178 rows x 4 columns]
    >>> air_quality = air_quality.sort_values("date.utc")    #按时间排序
    >>> air_quality
                           date.utc            location parameter  value
    2067  2019-05-07 01:00:00+00:00  London Westminster       no2   23.0
    1003  2019-05-07 01:00:00+00:00             FR04014       no2   25.0
    100   2019-05-07 01:00:00+00:00             BETR801      pm25   12.5
    1098  2019-05-07 01:00:00+00:00             BETR801       no2   50.5
    1109  2019-05-07 01:00:00+00:00  London Westminster      pm25    8.0
    ...                         ...                 ...       ...    ...
    2     2019-06-20 22:00:00+00:00             FR04014       no2   26.5
    102   2019-06-20 23:00:00+00:00  London Westminster      pm25    7.0
    1     2019-06-20 23:00:00+00:00             FR04014       no2   21.8
    101   2019-06-21 00:00:00+00:00  London Westminster      pm25    7.0
    0     2019-06-21 00:00:00+00:00             FR04014       no2   20.0
    
    [3178 rows x 4 columns]
    

    axis=0、axis=index,指的是遍历每个index、行号,即在纵向上遍历每列。axis=1、axis=columns,指的是遍历每个columns、列名,即在横向上遍历每行。

    相关文章

      网友评论

          本文标题:Python之pandas表格合并

          本文链接:https://www.haomeiwen.com/subject/wcmmuhtx.html