fschwartzer commited on
Commit
18438e9
1 Parent(s): bec769c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -7
app.py CHANGED
@@ -6,6 +6,8 @@ import numpy as np
6
 
7
  df = pd.read_csv('last_results_5.csv')
8
 
 
 
9
  image1 = 'images/rs_pmpa.PNG'
10
 
11
  title_html = """
@@ -162,14 +164,41 @@ if not filtered_df.empty:
162
  # Append these rows to the table data
163
  table_data = pd.concat([table_data, saude_row, educacao_row], ignore_index=True)
164
 
165
- # Calculate the sum of the last 12 periods for each "Conta" of each "Instituição"
166
- last_12_periods_sums = df.groupby(['Instituição', 'Conta']) \
167
- .apply(lambda x: x.sort_values(by='Period', ascending=False).head(12)['Value'].sum()) \
168
- .reset_index(name='Último ano')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
169
 
170
- # Merge this sum back into your table_data DataFrame
171
- # This assumes table_data already exists and contains columns for 'Instituição' and 'Conta'
172
- table_data = pd.merge(table_data, last_12_periods_sums, on=['Instituição', 'Conta'], how='left')
 
173
 
174
 
175
  # Display the table
 
6
 
7
  df = pd.read_csv('last_results_5.csv')
8
 
9
+ temp_data = pd.read_csv('temp_data(2).csv')
10
+
11
  image1 = 'images/rs_pmpa.PNG'
12
 
13
  title_html = """
 
164
  # Append these rows to the table data
165
  table_data = pd.concat([table_data, saude_row, educacao_row], ignore_index=True)
166
 
167
+ # Convert 'Data_Completa' to datetime format to ensure correct processing
168
+ temp_data['Data_Completa'] = pd.to_datetime(temp_data['Data_Completa'])
169
+
170
+ # Assuming 'Data_Completa' is sorted, if not, you should sort it.
171
+ # temp_data = temp_data.sort_values(by='Data_Completa', ascending=False)
172
+
173
+ # Initialize an empty list to store data including the 'Último ano' sums
174
+ data_with_last_year = []
175
+
176
+ # Iterate over each unique 'Instituição' and 'Conta' combination in 'df'
177
+ for instituicao in df['Instituição'].unique():
178
+ for conta in df[df['Instituição'] == instituicao]['Conta'].unique():
179
+ # Filter temp_data for the current 'Instituição' and 'Conta'
180
+ filtered_temp = temp_data[(temp_data['Instituição'] == instituicao) & (temp_data['Conta'] == conta)]
181
+
182
+ # Get the last 12 periods of 'Data_Completa'
183
+ last_12_periods = filtered_temp.nlargest(12, 'Data_Completa')
184
+
185
+ # Calculate the sum of 'Valor' for these periods
186
+ last_year_sum = last_12_periods['Valor'].sum()
187
+
188
+ # Append this information to the data list
189
+ data_with_last_year.append({
190
+ 'Instituição': instituicao,
191
+ 'Conta': conta,
192
+ 'Último ano': last_year_sum
193
+ })
194
+
195
+ # Convert the list to a DataFrame
196
+ last_year_data = pd.DataFrame(data_with_last_year)
197
 
198
+ # Merge this DataFrame with your existing table data to add the 'Último ano' column
199
+ # Assuming 'table_data' is your existing DataFrame that you want to add the column to
200
+ # You might need to adjust column names or merge keys based on your actual data structure
201
+ table_data = table_data.merge(last_year_data, on=['Instituição', 'Conta'], how='left')
202
 
203
 
204
  # Display the table