Hi, im trying to group by and get an average of two columns in Pandas then display inside Streamlit_Folium chropleth.geojson map but my KPI and Map averages are different.Trying to group by states and get each state average.
I have a RPM column which is only for dataframe purpose and i created by doing this:
for KPI i did this:
For chorplet map my code is like this:
But this is no go. KPI average 2.212 and Map average 2.26
KPI average is money / miles and Map average is sum of values and divides by the count and i need sum of money / sum of miles - same way as KPI
If i do:
I get this:
i get this:
got this:
Thank You.
I have a RPM column which is only for dataframe purpose and i created by doing this:
1 |
df[ "RPM" ] = df[ "Rate" ] / df[ "Miles" ] |
1 |
filtered_average_rpm = round (filtered_total_earnings / filtered_total_miles, 3 ) |
1 |
filtered_df[ "StateRPM" ] = filtered_df[ "RPM" ].groupby(filtered_df[ "PuState" ]).transform( 'mean' ) |
1 2 3 |
for feature in choropleth.geojson.data[ "features" ]: state_name = feature[ "properties" ][ "stusab" ] feature[ "properties" ][ "rpm" ] = "Average RPM: $" + str ( round (indexed_df.loc[state_name, "StateRPM" ].mean(), 2 )) if state_name in list (indexed_df.index) else '' |
KPI average is money / miles and Map average is sum of values and divides by the count and i need sum of money / sum of miles - same way as KPI
If i do:
1 |
filtered_df[ "StateRPM" ] = (filtered_df[ "Rate" ] / filtered_df[ "Miles" ]).groupby(filtered_df[ "PuState" ]).transform( 'mean' ) |
Error:TypeError: ufunc 'divide' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
If i do:1 |
filtered_df[ "StateRPM" ] = filtered_df[ "Rate" ] / filtered_df[ "Miles" ].groupby(filtered_df[ "PuState" ]) |
Error:ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 2 dimensions. The detected shape was (44, 2) + inhomogeneous part.
I also tried like this:1 |
filtered_df[ "RPM" ] = filtered_df[ "RPM" ].groupby(filtered_df[ "PuState" ]) |
Error:ValueError: Length of values (1) does not match length of index (58)
How do i get KPI`s average ?Thank You.