如何关联两个 pandas 数据帧的标量值

2024-01-06

如何关联两个 pandas 数据帧,为所有值找到一个 r 值?我不想关联列或行,而是关联所有标量值。一个数据帧是 x 轴,另一个数据帧是 y 轴。

我在这里下载了结构相同的 csv 文件:https://www.gapminder.org/data/ https://www.gapminder.org/data/这些表的列为年份,行为国家/地区,每个表报告的指标都有数值。

例如,我想了解政治参与指标(gapminder 将其称为指数,但我不想将其与数据框索引混淆)与政府职能指标(按年份)的整体关联and国家。

pol_partix_idx_EIU_df = pd.read_csv('polpartix_eiu.csv',index_col=0)
govt_idx_EIU_df = pd.read_csv('gvtx_eiu.csv',index_col=0)

pol_partix_idx_EIU_df.head()

    2006    2007    2008    2009    2010    2011    2012    2013    2014    2015    2016    2017    2018
country                                                 
Afghanistan 0.222   0.222   0.222   0.250   0.278   0.278   0.278   0.278   0.389   0.389   0.278   0.278   0.444
Albania 0.444   0.444   0.444   0.444   0.444   0.500   0.500   0.500   0.500   0.556   0.556   0.556   0.556
Algeria 0.222   0.194   0.167   0.223   0.278   0.278   0.389   0.389   0.389   0.389   0.389   0.389   0.389
Angola  0.111   0.250   0.389   0.416   0.444   0.444   0.500   0.500   0.500   0.500   0.556   0.556   0.556
Argentina   0.556   0.556   0.556   0.556   0.556   0.556   0.556   0.556   0.556   0.611   0.611   0.611   0.611

您可以按列或行关联:

pol_partix_idx_EIU_df.corrwith(govt_idx_EIU_df, axis=0)

2006    0.738297

2007    0.745321

2008    0.731913

...

2018    0.718520

dtype: float64


pol_partix_idx_EIU_df.corrwith(govt_idx_EIU_df, axis=1)

country

Afghanistan    6.790123e-01

Albania       -5.664265e-01

...

Zimbabwe       4.456537e-01

Length: 164, dtype: float64

但是,我想要一个 r 值来将一个表中的每个字段与另一个表中的每个相应字段进行比较。本质上,我想要这个散点图的 r 值:

plt.scatter(pol_cultx_idx_EIU_df,govt_idx_EIU_df)
plt.xlabel('Political participation')
plt.ylabel('Government functioning')

(示例代码不会像这样对图进行着色,但会绘制相同的点。)

问题的第二部分是如何对结构不完全相同的表执行此操作。我想要比较的每个表(数据框)都有国家/地区记录和年份列,但并非所有表都具有相同的国家/地区或年份。在上面的例子中,他们确实这样做了。如何仅获取数据帧的共享行和列的单个 r 值?


我模拟了一个我认为模仿你的设置——三个数据框,其中国家/地区跨行,年份跨列。然后,我将不同的数据集连接到一个数据框中。并展示如何计算它们之间的相关性。如果此示例的某些内容与您的设置不匹配,请告诉我。

import pandas as pd

set1 = pd.DataFrame({1980:[4, 11, 0], 1981:[5, 10, 2], 1982:[0, 3, 1]},
    index=pd.Index(['USA', 'UK', 'Iran'], name='country'))
set1.columns.name = 'year'
set1
year     1980  1981  1982
country                  
USA         4     5     0
UK         11    10     3
Iran        0     2     1
set2 = pd.DataFrame({1981:[2, 1, 10], 1982:[15, 1, 12], 1983:[10, 13, 1]},
    index=pd.Index(['USA', 'UK', 'Turkey'], name='country'))
set2.columns.name = 'year'
set2
year     1981  1982  1983
country                  
USA         2    15    10
UK          1     1    13
Turkey     10    12     1

请注意,与您的设置一样,某些国家/年份不存在于不同的数据集中。

set3 = pd.DataFrame({1980:[12, 11, 4], 1982:[9, 8, 11]},
    index=pd.Index(['USA', 'UK', 'Turkey'], name='country'))
set3.columns.name = 'year'

我们可以通过堆叠将它们变成多索引系列year然后使用跨列连接这些pd.concat.

df = pd.concat([set1.stack('year'), set2.stack('year'), set3.stack('year')],
    keys=['set1', 'set2', 'set3'], names=['set'], axis=1)
df
set           set1  set2  set3
country year                  
Iran    1980   0.0   NaN   NaN
        1981   2.0   NaN   NaN
        1982   1.0   NaN   NaN
Turkey  1980   NaN   NaN   4.0
        1981   NaN  10.0   NaN
        1982   NaN  12.0  11.0
        1983   NaN   1.0   NaN
UK      1980  11.0   NaN  11.0
        1981  10.0   1.0   NaN
        1982   3.0   1.0   8.0
        1983   NaN  13.0   NaN
USA     1980   4.0   NaN  12.0
        1981   5.0   2.0   NaN
        1982   0.0  15.0   9.0
        1983   NaN  10.0   NaN

我们可以计算三个不同集合的 3x3 相关矩阵。

df.corr()
set       set1      set2      set3
set                               
set1  1.000000 -0.723632  0.509902
set2 -0.723632  1.000000  0.606891
set3  0.509902  0.606891  1.000000
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何关联两个 pandas 数据帧的标量值 的相关文章

随机推荐