Home > Back-end >  How to create a column by unique ID with datetime index in a pandas dataframe [duplicate]
How to create a column by unique ID with datetime index in a pandas dataframe [duplicate]

Time:09-22

I have a database where I query for this kind of result with panda read_sql (millions results by query), Id is linked to an other table .


ID Date Value
369 2021-06-15 13:06:54 0.33
370 2021-06-15 13:06:54 0.02
377 2021-06-15 13:06:54 0.30
378 2021-06-15 13:06:54 0.36
390 2021-06-15 13:06:54 535.27
391 2021-06-15 13:06:54 35.55
264 2021-06-15 13:06:55 3.29
265 2021-06-15 13:06:55 5.70
266 2021-06-15 13:06:55 6.37
267 2021-06-15 13:06:55 23.36
268 2021-06-15 13:06:55 25.44
269 2021-06-15 13:06:55 23.80
270 2021-06-15 13:06:55 26.86
271 2021-06-15 13:06:55 22.54
272 2021-06-15 13:06:55 25.24

Is there a way to create a column by Id with the Date as unique Index in a pandas dataframe with value = None if there is no entry for this date like :

Date 369 370 377 ... 272
2021-06-15 13:06:54 0.33 0.02 0.30 ... None
2021-06-15 13:06:55 None None None ... 25.24

CodePudding user response:

Use pivot_table:

df.pivot_table('Value', 'Date', 'ID')
  • Related