1

I have a nested list in this format mydata=[[01/01/20,['point1','point2','point3,...]],[02/01/20,['point1','point2','point3']],...] I want to create a pandas dataframe grouped by date with every point as a different row. I have tried manually adding each row through a for loop, but other than taking more than an hour, the dataframe ended up being empty. Not sure how to go about this. Can I create the grouped dataframe directly from the list?

My desired output would be:

    date          points

    01/01/20      point1
                  point2
                  point3
    02/01/20      point1
                  point2
                  point3

1 Answer 1

1

The expected output is not fully clear, but if I guess correctly:

mydata = [['01/01/20',['point1','point2','point3']],
          ['02/01/20',['point1','point2','point3']]]

df = pd.DataFrame(dict(mydata))

Or:

cols, data = zip(*mydata)

df = pd.DataFrame(zip(*data), columns=cols)

output:

  01/01/20 02/01/20
0   point1   point1
1   point2   point2
2   point3   point3
Sign up to request clarification or add additional context in comments.

8 Comments

This works great, thank you! How would I be able to pivot it so that I have it stacked with the dates on the left and points as individual rows on the right?
@Mary why don't you provide the exact explicit output in your question for clarity? ;)
Maybe you simply want pd.DataFrame.from_dict(dict(mydata), orient='index')?
Apologies, I have edited my question to show what output I'm looking for
OK, then pd.DataFrame(dict(mydata)).melt() or pd.DataFrame(dict(mydata)).stack()
|

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.