Unable to create map visual using country names in Streamlit

Hello Team,


i need urgent help on map visual. I have checked all blogs the map visuals are displaying using LONGITUTE & LATITUDE. but my case is need to plot the map using country names. could you please help me on this asap. using below code

import folium
import pandas as pd
from datetime import datetime
from datetime import timedelta
import requests
import time
import random

import numpy as np
import os
import sys
from google.cloud import bigquery
import json
from datetime import date, timedelta
import pickle
from google.cloud import bigquery
import streamlit as st

Use parsed JSON into env instead of file

from google.oauth2 import service_account
from google.oauth2 import service_account
from google.analytics.data_v1beta import BetaAnalyticsDataClient

import pandas as pd
from google.analytics.data_v1beta.types import (
Dimension,
Metric,
RunRealtimeReportRequest,
)

def ga4_realtime_portal_app():

credentials = service_account.Credentials.from_service_account_file(
    "C:\\Users\\thim\\Downloads\\GA_analytics.json"    #change this to your path
)

client = BetaAnalyticsDataClient(credentials=credentials)

# Execute GA4 request
property_id = "216443556"
request_main_site = RunRealtimeReportRequest(
    property=f"properties/{property_id}",
    dimensions=[Dimension(name="platform"), Dimension(name="minutesAgo"), Dimension(name="city"), Dimension(name="country")],
    metrics=[Metric(name="activeUsers")],
)
response = client.run_realtime_report(request_main_site)
#response = client.run_report(request)
# Extract GA4 data into pandas DataFrame
data = []
for row in response.rows:
    dimension_values = [value.value for value in row.dimension_values]
    metric_values = [value.value for value in row.metric_values]
    data.append(dimension_values + metric_values)

columns = [dimension.name for dimension in response.dimension_headers] + [metric.name for metric in response.metric_headers]
df_main_site = pd.DataFrame(data, columns=columns)
print('*****Portal ********')
print(df_main_site.shape)
print(df_main_site)


#####  eshop ##########

property_id = "280758000"
request_eshop_site = RunRealtimeReportRequest(
    property=f"properties/{property_id}",
    dimensions=[Dimension(name="platform"), Dimension(name="minutesAgo"), Dimension(name="city"), Dimension(name="country")],
    metrics=[Metric(name="activeUsers")],
)
response = client.run_realtime_report(request_eshop_site)
#response = client.run_report(request)
# Extract GA4 data into pandas DataFrame
data = []
for row in response.rows:
    dimension_values = [value.value for value in row.dimension_values]
    metric_values = [value.value for value in row.metric_values]
    data.append(dimension_values + metric_values)

columns = [dimension.name for dimension in response.dimension_headers] + [metric.name for metric in response.metric_headers]
df_eshop_site = pd.DataFrame(data, columns=columns)
print('**********eshop**************')
print(df_eshop_site.shape)
print(df_eshop_site)

#######  concatenate ################

df_total  = pd.concat([df_main_site, df_eshop_site], axis = 0)
print('concatenated')
print(df_total.columns)

df_app = df_main_site[df_main_site['platform'].isin(['Android', 'iOS'])]

df_total['activeUsers'] = df_total['activeUsers'].astype(int)
df_app['activeUsers'] = df_app['activeUsers'].astype(int)



total_active_users = pd.DataFrame({'total_active_users' : [df_total['activeUsers'].sum()]})
app_active_users = pd.DataFrame({'app_active_users' : [df_app['activeUsers'].sum()]})



print(total_active_users)
print(app_active_users)


print(df_total.shape)
print(df_app.shape)

print(df_total.head())
print(df_app.head())



df_app_30_minutes_APP = df_app.groupby('minutesAgo').agg({'activeUsers': 'sum'}).reset_index()
df_total_30_minutes_ADC = df_total.groupby('minutesAgo').agg({'activeUsers': 'sum'}).reset_index()

print(df_app_30_minutes_APP.head())

print(df_total_30_minutes_ADC.head())

df_app_30_minutes_APP_map = df_app.groupby(['country','city']).agg({'activeUsers': 'sum'}).reset_index()
df_total_30_minutes_ADC_map = df_total.groupby(['country','city']).agg({'activeUsers': 'sum'}).reset_index()

return total_active_users, app_active_users, df_app_30_minutes_APP, df_total_30_minutes_ADC,df_app_30_minutes_APP_map,df_total_30_minutes_ADC_map

##total_active_users KPI
total_active_users, app_active_users, df_app_30_minutes_APP, df_total_30_minutes_ADC,df_app_30_minutes_APP_map,df_total_30_minutes_ADC_map = ga4_realtime_portal_app()
data_json = bytes(df_app_30_minutes_APP_map.to_json(orient=‘records’), encoding=‘utf-8’)
print(“JSON dataset”, data_json)

def display_map( df_app_30_minutes_APP_map, country, activeUsers):
#df = df[(df[‘country’] == country) & (df[‘activeUsers’] == activeUsers)]

#st.write(df.columns)
map = folium.Map(location=[38,-96.5], zoom_start=4)

choropleth = folium.Choropleth(
    geo_data=df_app_30_minutes_APP_map,
    data=df_app_30_minutes_APP_map,
    columns = ('country', 'activeUsers'),
    key_on = 'feature.properties.name'
)
choropleth.geojson.add_to(map)

st_map = st_folium(map, width=700, height=450)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.