gpt4 book ai didi

python - 如何将每一行对与前一列相关联?

转载 作者:行者123 更新时间:2023-12-04 17:13:19 25 4
gpt4 key购买 nike

我有以下数据框:

mydata

(形状为 72,22)

我想每年将每个国家与其他国家相关联。这将产生 21 个形状为 72*72 的数据帧。我想令我困惑的是相关性被定义为变量之间变化的关系,我不确定如何移动数据框以将当前年份与上一年进行比较(因此是 21 而不是 22)。

我试过了

corr = {}
for x in dfpivot.columns:
corr[x] = dfpivot[x].corr()

corr = {}
for x in dfpivot.T.index:
corr[x] = dfpivot.T.loc[x].corr()

我得到 TypeError: corr() missing 1 required positional argument: 'other'

所以我做了:

corr = {}
for x in dfpivot.columns:
corr[x] = dfpivot[x].corr(dfpivot.loc[:,x])

但这会将每一行与其自身相关联(这意味着我得到所有值为 1 的值)。

所以最后一个在我看来应该起作用,但它不起作用。为什么每年返回一个值?:

corr = {}
for x in dfpivot.columns:
for y in dfpivot.columns[1:]:
corr[x] = dfpivot[x].corr(dfpivot.loc[:,y])

结果:

{'1999-01-01': -0.7847692673880999,
'2000-01-01': 0.5179357977713173,
'2001-01-01': -0.8006230706819144,
'2002-01-01': -0.8608851552658657,
'2003-01-01': -0.23298450629551196,
'2004-01-01': -0.792648030305533,
'2005-01-01': 0.6711413744370501,

有人能帮忙吗?

数据:

['1999-01-01',
'2000-01-01',
'2001-01-01',
'2002-01-01',
'2003-01-01',
'2004-01-01',
'2005-01-01',
'2006-01-01',
'2007-01-01',
'2008-01-01',
'2009-01-01',
'2010-01-01',
'2011-01-01',
'2012-01-01',
'2013-01-01',
'2014-01-01',
'2015-01-01',
'2016-01-01',
'2017-01-01',
'2018-01-01',
'2019-01-01',
'2020-01-01']
['Africa',
'All Countries Total',
'Argentina',

[ 2299., -1538.,    nan, -1851.,  1604., -1827., -2047.,  -216.,
985., 1338., 4694., 16., -2143., 2830., -2395., 140.,
-406., 5675., 1110., -2973., -1380., 1414.]
[ 61756., -12431.,  11624.,  26483.,  -6609.,  20039., -15386.,
-21390., -17339., -31049., 48324., -41960., 17528., 17136.,
-12768., 2743., -17969., -20280., -38804., 90313., -98720.,
-66081.]
[  914.,   137.,   151.,  -623.,  -693.,   634.,    nan,    nan,
nan, -71., 427., -3659., nan, nan, 452., 443.,
-495., -1097., 557., -5454., 910., nan]

最佳答案

我可能不适合这里,但听起来你并不喜欢 corr()。我的理解是,您想对由当年(2020 年)和另外一年组成的数据框执行 corr()。每个数据框中只有 2 列。不够?如果你想这样做,它每年只会返回 1s 或 -1s,至少这是我每年得到的结果:

让我们转置您的数据框:

df2 = df.transpose()

现在看起来更像是这样:

          Africa    All C...Total   Argentina
1999-01-01 2299.0 61756.0 914.0
2000-01-01 -1538.0 -12431.0 137.0
2001-01-01 NaN 11624.0 151.0
2002-01-01 -1851.0 26483.0 -623.0
2003-01-01 1604.0 -6609.0 -693.0
2004-01-01 -1827.0 20039.0 634.0
2005-01-01 -2047.0 -15386.0 NaN
2006-01-01 -216.0 -21390.0 NaN
2007-01-01 985.0 -17339.0 NaN
...

显然我们需要一个列表来存储我们的数据帧(在循环中命名它们不是 pythonic - 我刚刚从 this answer 中了解到)

list_of_df = list()  # <-- here we're going to store the dataframes for each year
index_list = df2.index
current_year = index_list[-1]
for i in range(len(index_list)):
df_corelated = df2.loc[[index_list[i] , current_year]].corr()
list_of_df.append(df_corelated)

现在列表中的所有内容都已准备就绪,想要...说第四年?:

list_of_df[3]
               Africa   All Countries Total Argentina
Africa 1.0 -1.0 NaN
All Countries Total -1.0 1.0 NaN
Argentina NaN NaN NaN

只有 1 和 -1

关于python - 如何将每一行对与前一列相关联?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69084554/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com