Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
90 views
in Technique[技术] by (71.8m points)

python - Dataframe and loops and lists

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
question from:https://stackoverflow.com/questions/65946440/dataframe-and-loops-and-lists

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
import pandas as pd

# Use your data instead of this sample df
df = pd.DataFrame({'Dates':['2016-01-01 02:00:00','2016-01-01 03:00:00','2016-01-01 04:00:00',
                            '2016-01-01 05:00:00','2016-01-01 06:00:00'],
                   'Ramp_1':[-823.0,-899.0,-652.0,-304.0,99.0],
                   'Ramp_2':[-566.47,-586.54,-473.33,-178.20,273.08]})

df['Dates'] = pd.to_datetime(df['Dates'], format="%Y-%m-%d %H:%M")

# This is a list with the 12 DataFrames that you want
dfs_by_month = [df[df['Dates'].apply(lambda x: x.month)==month] for month in range(1,13)]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...