I am getting a warning on the code below. It seems to relate to both the line where I'm inserting a new column and also to the loop. I have seen other posts relating to this error but unfortunately I'm a complete novice and can't see what the issue is with mine. The warning specifically says "Try using .loc... instead", but this is what I'm doing so I don't know what the problem is.
I'm using pycharm and a snip of the dataframe before the Column addition and loop is below.
Any help would be appreciated Thanks
import numpy as np
import pandas as pd
gdp_data = pd.read_csv("GDP Hist.csv")
# data has 2 entries per year for: total in millions & GDP per person
# removing duplicates based on years to leave only total GDP
gdp_data.drop_duplicates(subset=["LOCATION", "TIME"], inplace=True)
# Create list of unneeded columns & remove
unneeded_cols = ["INDICATOR", "SUBJECT", "MEASURE", "FREQUENCY", "Flag Codes"]
gdp_data.drop(columns=unneeded_cols, axis=1, inplace=True)
# print(gdp_data.info())
# Subset for Ireland GDP
gdp_ire = gdp_data[gdp_data['LOCATION'] == "IRL"]
gdp_ire.set_index('TIME', inplace=True)
gdp_ire['Annual%'] = np.nan # insert blank column
# loop through dataframe & calc annual % growth
for i in gdp_ire.index:
if i == 1970:
gdp_ire.loc[i, 'Annual%'] = ""
else:
gdp_ire.loc[i, 'Annual%'] = (gdp_ire.loc[i, 'Value']-gdp_ire.loc[i-1, 'Value'])/gdp_ire.loc[i-1, 'Value']*100
print(gdp_ire)