Home > Blockchain >  Creating an array to include repeated dates for IDs
Creating an array to include repeated dates for IDs

Time:09-06

I have a CSV file containing these datas[1]: https://i.stack.imgur.com/R1LxR.png

I would like to restructure it into a pandaframe as shown in the snapshot: https://i.stack.imgur.com/UjwNz.jpg where the dates(19800101 - 20211231) are repeated for each rainfall station.

Apologies for submitting photos/snapshots as I do not know how to upload CSV files to stackoverflow.

Thank you so much!

CodePudding user response:

Use melt:

Unpivot a DataFrame from wide to long format, optionally leaving identifiers set

import pandas as pd

d = {'Station':['A','B'], 'Elevation':[100,150], '19800101':[0,1],'19800102':[None,99]}

df = pd.DataFrame(d)
#  Station  Elevation  19800101  19800102
#0       A        100         0       NaN
#1       B        150         1      99.0

df = df.melt(id_vars=['Station','Elevation'], var_name='Date', value_name='Rainfall (mm)')
#   Station  Elevation      Date  Rainfall (mm)
# 0       A        100  19800101            0.0
# 1       B        150  19800101            1.0
# 2       A        100  19800102            NaN
# 3       B        150  19800102           99.0
  • Related