Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exploding nested dictionary #3

Open
dinahrabe opened this issue Oct 11, 2022 · 2 comments
Open

Exploding nested dictionary #3

dinahrabe opened this issue Oct 11, 2022 · 2 comments

Comments

@dinahrabe
Copy link

I constructed a nested dictionary with all information of the books, poems, stanza and lines. In total it has 4 layers (book, poem title, stanza number, lines). I tried to use stack() and explode() to transform it into a dataframe. But it only works if there are only 3 layers (so for each book separately). Now with the 4 layers, it removes the "deepest" level when trying to explode() it. Someone any clue/idea on that?

@mcallaghan
Copy link
Owner

Hm, I don't know if I have ever "exploded" a dictionary. Can you give an example of your data structure?

@mcallaghan
Copy link
Owner

My solution for getting things into pandas is often a list of dictionaries, which looks like this:

[   {   'book': 'SONGS OF INNOCENCE',
        'line_n': 0,
        'poem': 'INTRODUCTION',
        'stanza_n': 0,
        'text': 'Piping down the valleys wild,'},
    {   'book': 'SONGS OF INNOCENCE',
        'line_n': 1,
        'poem': 'INTRODUCTION',
        'stanza_n': 0,
        'text': 'Piping songs of pleasant glee,'}]

Then you can simply do df = pd.DataFrame.from_dict.

Perhaps you can construct data like this by iterating through your levels?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants