Home > Software engineering >  Grouped pandas dataframe from nested list
Grouped pandas dataframe from nested list

Time:10-20

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

CodePudding user response:

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
  • Related