Python合并数据帧警告

2024-09-27 04:28:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我在合并2个数据帧时收到此警告,其中1个数据帧是从枢轴创建的:

UserWarning: merging between different levels can give an unintended result (1 levels on the left,2 on the right) warnings.warn(msg, UserWarning)

我不明白问题是什么,因为两个数据帧都是从相同的原始数据创建的(不过我是一个完全的新手)。代码节略如下:

    import numpy as np
    import pandas as pd
    
    gdp_totals = gdp_data.pivot_table(values="Value", index="TIME", aggfunc=[np.sum, np.mean, np.median])
    print(gdp_totals.info())

    gdp_ire = gdp_data[gdp_data['LOCATION'] == "IRL"].copy()
    gdp_ire.set_index('TIME', inplace=True)
    gdp_ire['Annual%'] = np.nan       # insert blank column
    for i in gdp_ire.index:
        if i == 1970:
            gdp_ire.loc[i, 'Annual%'] = 0
        else:
            gdp_ire.loc[i, 'Annual%'] = (gdp_ire.loc[i, 'Value']-gdp_ire.loc[i-1, 'Value'])/gdp_ire.loc[i-1, 'Value']*100

    gdp_all = gdp_ire.merge(gdp_totals, on='TIME', suffixes=['_IRE', '_TOTALS'], how='left')

支点的剪断:

pivot snip

国内生产总值的削减:

ire snip

数据帧的形状:

    print(gdp_totals.shape)
    print(gdp_ire.shape)

shapes

我正在细分的原始数据片段:

original data


Tags: the数据dataindextimevalueonnp

热门问题