I'm trying to finish my workproject but I'm getting stuck at a certain point.
Part of the dataframe I have is this:
year_month | year | month |
---|---|---|
2007-01 | 2007 | 1 |
2009-07 | 2009 | 7 |
2010-03 | 2010 | 3 |
However, I want to add the column "season". I'm illustrating soccer seasons and the season column needs to illustrate what season the players plays. So if month is equal or smaller than 3, the "season" column needs to correspond with ((year-1), "/", year) and if larger with (year, "/", (year + 1)). The table should look like this:
year_month | year | month | season |
---|---|---|---|
2007-01 | 2007 | 1 | 2006/2007 |
2009-07 | 2009 | 7 | 2009/2010 |
2010-03 | 2010 | 3 | 2009/2010 |
Hopefully someone else can help me with this problem.
Here is the code to create the first Table:
import pandas as pd
from datetime import datetime
df = pd.DataFrame({'year_month':["2007-01", "2009-07", "2010-03"],
'year':[2007, 2009, 2010],
'month':[1, 7, 3]})
# convert the 'Date' columns to datetime format
df['year_month']= pd.to_datetime(df['year_month'])
Thanks in advance!