NumEconCopenhagen / lectures-2019

Slides and code used in the lectures
MIT License
13 stars 23 forks source link

A new column in dataframe #7

Open Gandalfearless opened 5 years ago

Gandalfearless commented 5 years ago

Hello

We use data from DST which we have converted into a Dataframe. We want to create a new column called NX which is the net export: NX=X-M.

But how do you do that? We can't figure it out...

image

code: import numpy as np import matplotlib.pyplot as plt import pandas as pd import pydst dst = pydst.Dst(lang = 'da')

columns_dict = {} columns_dict['TRANSAKT'] = 'variable' columns_dict['PRISENHED'] = 'unit' columns_dict['TID'] = 'year' columns_dict['INDHOLD'] = 'value'

var_dict = {} # var is for variable

var_dict['P.1 Output'] = 'Y'

var_dict['P.3 Final consumption expenditure'] = 'C'

var_dict['P.3 Government consumption expenditure'] = 'G'

var_dict['P.5 Gross capital formation'] = 'I'

var_dict['P.6 Export of goods and services'] = 'X'

var_dict['P.7 Import of goods and services'] = 'M'

VARIABLES

var_dict['B.1*g Bruttpmationalprodukt, BNP'] = 'Y' var_dict['P.31 Privatforbrug'] = 'C' var_dict['P.31 Forbrugsudgifter i non-profit institutioner rettet mod husholdninger (NPISH)'] = 'NPISH' var_dict['P.3 Offentlige forbrugsudgifter'] = 'G' var_dict['P.5g Bruttoinvesteringer'] = 'I' var_dict['P.6 Eksport af varer og tjenester'] = 'X' var_dict['P.7 Import af varer og tjenester'] = 'M'

UNITS

unit_dict = {}

unit_dict['2010-prices, chained values'] = 'real'

unit_dict['Current prices'] = 'nominal'

unit_dict['2010-priser, kædede værdier'] = 'realle' unit_dict['løbende priser'] = 'nominelle'

CODE

nan1 = dst.get_data(table_id = 'NAN1', variables={'TRANSAKT':[''], 'PRISENHED':[''], 'TID':['*']}) nan1.rename(columns = columns_dict, inplace = True)

for key,value in var_dict.items(): nan1.variable.replace(key,value,inplace=True)

for key,value in unit_dict.items(): nan1.unit.replace(key,value, inplace=True)

I = False for key,value in var_dict.items(): I = I | (nan1.variable == value) nan1 = nan1[I]

nan1.groupby(['variable','unit']).describe()

X = nan1["year"]>2000 #laver en ny variabel hvor X inderholder en variabel year som er større end 2000. giver nan1= nan1[x] for at sige at den skal være lig med det. nan1 = nan1[X]

Y=nan1["unit"]=="Pr. indbygger, løbende priser, (1000 kr.)" nan1 = nan1[Y]

df=pd.DataFrame(data=nan1)

df.sort_values(by=["year","variable"])

print(df)

JeppeDruedahl commented 5 years ago

Sorry for not answering. I had set up my alert e-mails wrong.

I hope you have solved your problem. Otherwise, please write again.

A new variable in a dataframe is created by:

df['NX'] = df['X']-df['M']