【问题标题】:Alter value in pandas dataframe based on corresponding value in another dataframe根据另一个数据框中的相应值更改熊猫数据框中的值
【发布时间】:2020-07-21 10:03:04
【问题描述】:

我有两个 pandas 数据框。

  1. 第一个 (df1) 有两列:“Country”(字符串)和“Population”(整数)。每行包含一个不同的国家/地区及其对应的人口(约 200 行)。
  2. 第二个 (df2) 也有两列:“Country”(字符串)和“Value”(int)。每个国家/地区以随机顺序出现的次数不定,并带有相应的值(数千行)。

我想将 df2['Value'] 中的每个值除以该行所在国家/地区的相应人口。

我的尝试:(假设有一个名为“国家”的列表,其中包含这些数据框中的所有国家/地区

for country in countries:
  val = df2.loc[df2['Country'] == country]['Values'] # All values corresponding to country
  pop = df1.loc[df1['Country'] == country]['Population'] # Population corresponding to country
  df2.loc[df2['Country'] == country]['Values'] = val / pop

有没有更好的方法来做到这一点?也许是不涉及 for 循环的解决方案?

谢谢

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    尝试以下方法:

    # Assuming that there are the same countries in both df    
    df3 = pd.merge(df2, df1, how = 'inner' on='Country')
    df3["Values2"] = df3["Values"] / df3["Population"]
    

    【讨论】:

      【解决方案2】:

      另一种实现是在应用除法运算符之前连接两个表。有点像:

      df2 = df2.join(df1,on='Country',how='left')
      df2['Values'] = df2['Values'] / df2['Population']
      

      【讨论】:

        【解决方案3】:

        您可以为此使用merge

        df3 = df2.merge(df1, on='Country') # maybe you want to use how='left'
        df3['Div'] = df3['Values'] / df3['Population']
        

        你可以阅读更多关于合并in the docs

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-08-04
          • 2019-06-30
          • 1970-01-01
          • 2013-10-14
          相关资源
          最近更新 更多