theRenaissanceMan
theRenaissanceMan

Reputation: 61

Alter value in pandas dataframe based on corresponding value in another dataframe

I have two pandas dataframes.

  1. The first (df1) has two columns: 'Country' (string) and 'Population' (int). Each row consists of a different country and its corresponding population (~200 rows).
  2. The second (df2) also has two columns: 'Country' (string) and 'Value' (int). Each country appears a variable number of times in random order with a corresponding value (thousands of rows).

I want to divide each value in df2['Value'] by the corresponding population of that row's country.

My attempt: (Assume there's a list called 'countries' containing all countries in these dataframes

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

Is there a better way to do this? Perhaps a solution that doesn't involve a for-loop?

Thanks

Upvotes: 0

Views: 77

Answers (3)

Vlad-Marius Griguta
Vlad-Marius Griguta

Reputation: 187

An alternative implementation would be to join the two tables before applying the division operator. Something on the line of:

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

Upvotes: 1

Elkana Bardugo
Elkana Bardugo

Reputation: 375

You can use merge for that:

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

You can read more about merge in the docs

Upvotes: 0

Let's try
Let's try

Reputation: 1058

Try the following:

# 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"]

Upvotes: 1

Related Questions