Pd.json_normalize / to_list doesn't work as expected

Hey there,

Today I came across a strange behaviour within streamlit. My code run in a script / notebook give a different output than when using the same in a streamlit app. In particular, I am trying to normalize dict object found in one specific column.

Here is how my df looks like:

sample_df = pd.DataFrame({'A': {0: 'a', 1: 'b', 2: 'c'},
                          'labels': {0: {"locale": "de", "value":"haus"}, 1: {"locale": "en", "value":"house"}, 2: {"locale": "it", "value":"casa"}},
                          'C': {0: 2, 1: 4, 2: 6}})

If i know run pd.json_normalize(sample_df["labels") on my local machine i will get a table / df like below:

locale value
0 de NICHT VERWENDEN - Exportiert von Manor
1 en Do not use - Exported from Manor
2 fr NE PAS UTILISER - Exporté de Manor
3 it NON USARE - Esportato da Manor
4 de Alle anderen Kategorien

On streamlit however i get something like this:

{locale:null,value:null} {locale:null,value:null} {locale:null,value:null} {locale:null,value:null}
0 {locale:de,value:NICHT VERWENDEN - Exportiert von Manor} {locale:en,value:Do not use - Exported from Manor} {locale:fr,value:NE PAS UTILISER - Exporté de Manor} {locale:it,value:NON USARE - Esportato da Manor}
1 {locale:de,value:Alle anderen Kategorien} {locale:en,value:All other categories} {locale:fr,value:Toutes les autres catégories} {locale:it,value:Tutte le altre categorie}
2 {locale:de,value:Baby & Kind (außer Kleidung)} {locale:en,value:Baby & child (excluding clothing)} {locale:fr,value:Bébé et enfant (à l’exclusion des vêtements)} {locale:it,value:Neonati e bambini (escluso abbigliamento)}
3 {locale:de,value:Schönheit & Wellness (ohne Elektroartikel)} {locale:en,value:Beauty & Wellness (excluding Electrical items)} {locale:fr,value:Beauté et bien-être (à l’exclusion des articles électriques)} {locale:it,value:Bellezza e benessere (esclusi gli articoli elettrici)}
4 {locale:de,value:Fisch - bearbeitet / verarbeitet} {locale:en,value:Fish - Prepared/Processed} {locale:fr,value:Poisson - Préparé/Transformé} {locale:it,value:Pesce - Lavorato/Trattato}
5 {locale:de,value:Kleidung & Mode} {locale:en,value:Clothing & Fashion} {locale:fr,value:Vêtements et mode} {locale:it,value:Abbigliamento e moda}

Not really sure what the issue is

Can you share the exact code you’re using, and the streamlit version?

When I try this code with streamlit 1.12.0

import pandas as pd
import streamlit as st

sample_df = pd.DataFrame(
    {
        "A": {0: "a", 1: "b", 2: "c"},
        "labels": {
            0: {"locale": "de", "value": "haus"},
            1: {"locale": "en", "value": "house"},
            2: {"locale": "it", "value": "casa"},
        },
        "C": {0: 2, 1: 4, 2: 6},
    }
)

st.write(pd.json_normalize(sample_df["labels"]))

I get this output
image

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