I am trying to solve this problem with my code – I have developed a function in order to figure out the sum total of a value for a particular year using pandas dataframes.
The code I have written shows no errors and such and seems to work but at the end wen I try to print the output to excel it just comes up blank no data at all just the column names appear in it.
I have tried numerous different approaches to try to get it to work so I am unsure on how best to solve this, any help would be appreciated.
Here is an example of my code I am not sure where I have gone wrong with it but any help would be much appreciated.
import pandas as pd
import openpyxl as op
df = pd.read_excel(r"C:UsersblakecarPycharmProjectsLFS DataMock File LFS.xlsx", sheet_name='Sheet1')
output = pd.DataFrame()
output['COUNTRY'] = " "
output["REGION_2D"] = " "
output["AC_Change"] = " "
output["PC_Change"] = " "
def func(COUNTRY):
print('----------------------------------')
print(COUNTRY)
print('----------------------------------')
filtered_df=df[df['COUNTRY'].isin(['AT'])]
filtered_df = filtered_df[filtered_df['variable_category'].isin(['1'])]
filtered_2018 = filtered_df[filtered_df['YEAR'].isin(['2018'])]
total_2018 = filtered_2018['value'].sum()
return total_2018
output.to_excel('Mock File.xlsx', index=False)
`
rogue1 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.