如何根据来自dfB的列/行值,用来自另一个数据框(dfB)的值填充熊猫数据框(dfA)列“ A”?

问题描述 投票:2回答:3

我有一个df(dfA),其6个国家/地区的出生时预期寿命和每年gdp。使用以下structure

country  year  expectancy  gdp  difference
chile    2000    60       1bn     NA
chile    2001    63       1.5bn  0.5bn
chile    2002    65       2.5bn  0.5bn
chile    2003    68       3.5bn  1.0bn
  .
  .
  .
chile    2015    80      10bn     10bn

[每一行代表一个国家/地区的每年数据(gdp,期望值等),范围是2000年至2015年,并包含6个国家/地区。

我创建了一个新的数据框来存储每个国家的重要总体变量,例如每个国家的GDP增量(2015年的GDP减去2000年的GDP)。新的df(dfB)看起来像this

country   startEndDelta (dummydata)
Chile        x
China        y
Germany      z
Mexico       a
USA          b
Zimbabwe     c

我想做的是在我的newdf中添加一个新列,该列显示每个国家的哪一年GDP增​​长幅度最大。

我已经可以计算年份,但是我首先必须创建一个仅包含一个国家/地区记录的数据框。 Here I do it the way I metioned before.

我希望这样做的方式类似于:

dfB['biggestDeltaYear'] = ?year with the biggest increase in GDP?

在我的新列'biggestDeltaYear'的dfB中,每一行代码都填充了dfB中的每一行。

我有什么选择?

非常感谢

python pandas dataframe
3个回答
2
投票

也许您可以尝试使用pandas.DataFrame的groupby()方法

dfA.groupby('country').apply(lambda x:x['year'].iloc[x['difference'].argmax()])

0
投票

这里是另一种选择:

dfA['biggestDeltaYear'] = (dfA.iloc[dfA.groupby('country')['difference']
                           .apply(lambda x: x.argmax())]['year'])

0
投票

您应该能够使用groupby并在熊猫中应用lambda操作。下面是我画的一个例子:

请考虑以下数据:

Country,Year,GDP
Chile,2011,1.5
Chile,2012,1
Chile,2013,2
Chile,2014,2.3
Chile,2015,3.2
Nigeria,2011,0.6
Nigeria,2012,0.9
Nigeria,2013,2.1
Nigeria,2014,2.2
Nigeria,2015,2.6
Australia,2011,10.4
Australia,2012,14.4
Australia,2013,12.3
Australia,2014,13.3
Australia,2015,15

首先,我们明智地将差异操作应用于国家/地区:

df['diff'] = df.groupby("Country")["GDP"].transform(pd.DataFrame.diff)

    Country     Year    GDP     diff
0   Chile       2011    1.5     NaN
1   Chile       2012    1.0     -0.5
2   Chile       2013    2.0     1.0
3   Chile       2014    2.3     0.3
4   Chile       2015    3.2     0.9
5   Nigeria     2011    0.6     NaN
6   Nigeria     2012    0.9     0.3
7   Nigeria     2013    2.1     1.2
8   Nigeria     2014    2.2     0.1
9   Nigeria     2015    2.6     0.4
10  Australia   2011    10.4    NaN
11  Australia   2012    14.4    4.0
12  Australia   2013    12.3    -2.1
13  Australia   2014    13.3    1.0
14  Australia   2015    15.0    1.7

然后我们可以根据最大值生成一个布尔列:

df['biggestDeltaYear'] = df.groupby("Country")['diff'].apply(lambda x:x==x.max())
    Country     Year    GDP     diff    biggestDeltaYear
0   Chile       2011    1.5     NaN     False
1   Chile       2012    1.0     -0.5    False
2   Chile       2013    2.0     1.0     True
3   Chile       2014    2.3     0.3     False
4   Chile       2015    3.2     0.9     False
5   Nigeria     2011    0.6     NaN     False
6   Nigeria     2012    0.9     0.3     False
7   Nigeria     2013    2.1     1.2     True
8   Nigeria     2014    2.2     0.1     False
9   Nigeria     2015    2.6     0.4     False
10  Australia   2011    10.4    NaN     False
11  Australia   2012    14.4    4.0     True
12  Australia   2013    12.3    -2.1    False
13  Australia   2014    13.3    1.0     False
14  Australia   2015    15.0    1.7     False

也可以使用:而不是布尔值来获得实际的年值:

df['Year'][df.groupby("Country")['diff'].apply(lambda x:x==x.max())]

df.iloc[df.groupby("Country")['diff'].apply(lambda x:x.idxmax())]['Year']

HTH。

© www.soinside.com 2019 - 2024. All rights reserved.