i want to combine months from years into sequence, for example, i have dataframe like this:
JavaScript
x
8
1
stuff_id date
2
1 2015-02-03
3
2 2015-03-03
4
3 2015-05-19
5
4 2015-10-13
6
5 2016-01-07
7
6 2016-03-20
8
i want to sequence the months of the date. the desired output is:
JavaScript
1
8
1
stuff_id date month
2
1 2015-02-03 1
3
2 2015-03-03 2
4
3 2015-05-19 4
5
4 2015-10-13 9
6
5 2016-01-07 12
7
6 2016-03-20 14
8
which means feb’15 is the first month in the date list and jan’2016 is the 12th month after feb’2015
Advertisement
Answer
If your date
column is a datetime (if it’s not, cast it to one), you can use the .dt.month
and .dt.year
properties for this!
https://pandas.pydata.org/docs/reference/api/pandas.Series.dt.month.html
recast
(text copy from Answer to Pasting data into a pandas dataframe)
JavaScript
1
15
15
1
>>> df = pd.read_table(io.StringIO(s), delim_whitespace=True) # text from SO
2
>>> df["date"] = pd.to_datetime(df["date"])
3
>>> df
4
stuff_id date
5
0 1 2015-02-03
6
1 2 2015-03-03
7
2 3 2015-05-19
8
3 4 2015-10-13
9
4 5 2016-01-07
10
5 6 2016-03-20
11
>>> df.dtypes
12
stuff_id int64
13
date datetime64[ns]
14
dtype: object
15
extract years and months to decimal months and reduce to relative
JavaScript
1
11
11
1
>>> months = df["date"].dt.year * 12 + df["date"].dt.month # series
2
>>> df["months"] = months - min(months) + 1
3
>>> df
4
stuff_id date months
5
0 1 2015-02-03 1
6
1 2 2015-03-03 2
7
2 3 2015-05-19 4
8
3 4 2015-10-13 9
9
4 5 2016-01-07 12
10
5 6 2016-03-20 14
11