简体   繁体   中英

fill a new column with the division of data of a column in a Excel Sheet by looking up the denominator value from other Sheet

i have a 2 dataframes as given below,

import pandas as pd
restaurant = pd.read_excel("C:/Users/Avinash/Desktop/restaurant data.xlsx")
restaurant

Restaurant          StartYear Capex     inflation_adjusted_capex
Bawarchi Restaurant 1986      6000      Nan
Ks Baker's          1988      2000      Nan
Rajesh Restaurant   1989      1050      Nan
Ahmed Steak House   1990      9000      Nan
Absolute Barbique   1997      9500      Nan

inflation = pd.read_excel("C:/Users/Avinash/Desktop/restaurant data.xlsx", sheet_name="Sheet2")
inflation

Years Inflation_Factor 
1985   0.111   
1986   0.134   
1987   0.191   
1988   0.2253
1989   0.265
1990   0.304

Aim: is to fill "inflation_adjusted_capex" with div of "Capex" by corresponding years "Inflation_Factor from second Dataframe.

The code i wrote is,

for i in restaurant["StartYear"]:
   restaurant["inflation_adjusted_capex"] = 
        (restaurant["inflation_adjusted_capex"])/(inflation[inflation["Years"] == i]["Inflation_Factor"])
print(restaurant["inflation_adjusted_capex"])

0  Nan
1  Nan
2  Nan
3  Nan
4  Nan
Name: Inflation adjusted Capex to current year, dtype: float64

Unfortunately this code is returning Nan values, kindly help me. Thanks in advance.

There are a couple ways to do this. The first is to join the dataframes so that you have your inflation factors in the first dataframe, and then do the calculation:

#add inflation_factor column to first dataframe
restaurant = restaurant.merge(inflation, left_on = 'StartYear', right_on = 'Year')
#do dividsion
restaurant['inflation_adjusted_capex'] = restaurant['Capex']/restaurant['Inflation_Factor']

The other is to apply a function that behaves like an excel VLOOKUP :

#set year as index for inflation so we can look up based on it
inflation = inflation.set_index('Year')
#look up inflation factor and divide with a lambda function
restaurant['inflation_adjusted_capex'] = inflation.apply(lambda row: row['Capex']/inflation['Inflation_Factor'][row['StartYear']], 1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM