I wanted to know if there is a more concise way to write the code below. I basically have a dataframe with three columns: "Dates", "Ramp_1", "Ramp_2". My goal is to separate the data (Ramp_1 and Ramp_2) by month. The dates contain multiple years of data in this form: %Y-%m-%d %H:%M. I created a new column with only the months and then I used df.loc to create new dataframes:
mon=['jan','feb','mar','apr', 'may', 'jun','jul','aug','sep','oct','nov','dec']
for i in range(len(mon)):
mon[i] = df.loc[df["month"]==(i+1), ["Ramp_1","Ramp_2"]]
I wanted to create 12 new dataframes with each one named after a month. Instead of doing that, I ended up creating a list of dataframes. So I manually wrote the code like this:
jan=mon[0]
feb=mon[1]
mar=mon[2]
apr=mon[3]
may=mon[4]
jun=mon[5]
jul=mon[6]
aug=mon[7]
sep=mon[8]
octo=mon[9]
nov=mon[10]
dec=mon[11]
My question is: is there a more concise way to write this? I know there is, but i haven't been able to figure it out!
I also tried doing this:
mon=['jan','feb','mar','apr', 'may', 'jun','jul','aug','sep','oct','nov','dec']
for i in range(len(mon)):
name = mon[i]
name = df.loc[df["month"]==(i+1), ["Ramp_1","Ramp_2"]]
sample of my data:
Dates Ramp_1 Ramp_2 month
0 2016-01-01 02:00:00 -823.0 -566.47 1
1 2016-01-01 03:00:00 -899.0 -586.54 1
2 2016-01-01 04:00:00 -652.0 -473.33 1
3 2016-01-01 05:00:00 -304.0 -178.20 1
4 2016-01-01 06:00:00 99.0 273.08 1
... ... ... ... ...
35045 2019-12-31 11:00:00 -613.0 -793.54 12
35046 2019-12-31 12:00:00 -311.0 -1159.81 12
35047 2019-12-31 13:00:00 -530.0 -964.18 12
35048 2019-12-31 14:00:00 79.0 538.85 12
35049 2019-12-31 15:00:00 181.0 574.21 12