0

I'm trying to create a dictionary from a nested data structure (taxonomy) as follows:

pd.DataFrame({'genus':  ['Unknown', 'External Metal'], 
              'species':['Other Feature/Anomaly', 
                        ['Close External Metal', 'Touching Metal Object']]})

I get the following as output:

            genus                                        species
0         Unknown                          Other Feature/Anomaly
1  External Metal  [Close External Metal, Touching Metal Object]

Here's what I want:

            genus                                        species
0         Unknown                          Other Feature/Anomaly
1  External Metal                           Close External Metal 
2  External Metal                          Touching Metal Object

Or perhaps there is a better way of thinking about structuring these sorts of hierarchies using Pandas? I'm not opposed to having the output structured differently than I have indicated above as long as the hierarchy is properly maintained and I can easily navigate that hierarchy Thanks!

1 Answer 1

1

Let's use apply, pd.Series, and stack:

df.set_index('genus').species.apply(pd.Series).stack().reset_index(name='Species').drop('level_1', axis=1)

Output:

            genus                Species
0         Unknown  Other Feature/Anomaly
1  External Metal   Close External Metal
2  External Metal  Touching Metal Object
Sign up to request clarification or add additional context in comments.

1 Comment

Great answer! Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.