Issue
I have a set of time series data that's looking at average windspeed by month. I am trying to write some code so that I can:
- Separate months into dry (May to October) and wet (November to April) seasons.
- Generate two different files with the dry seasons and wet seasons of each year in each.
I'm fairly new to python so any help would be appreciated.
Solution
Assuming you have your data in a pandas Dataframe df
.
import pandas as pd
# parse the date to datetime:
date = pd.to_datetime(df['date'], format="%Y-%m")
# Get month numbers for the two seasons
wet_months = [11, 12, 1, 2, 3, 4]
dry_months = [month for month in range(1, 13) if month not in wet_months]
# Now partition the data into two tables
# Select each row that has a month in the given season using `.loc`.
df_wet = df.loc[date.dt.month.isin(wet_months)]
df_dry = df.loc[date.dt.month.isin(dry_months)]
# dataframe index seems to be arbitrary in your question
# so I'd probably drop indexes before saving to file. Or save the file without index.
df_wet = df_wet.reset_index(drop=True)
df_dry = df_dry.reset_index(drop=True)
# This way the tables get new range indexes starting from zero
# instead of remembering their index from the original `df`.
# now go ahead and save to .csv or any other output format
df_wet.to_csv("wet.csv")
df_dry.to_csv("dry.csv")
Answered By - creanion
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.