'Adding a random column with years
I am working with Python on a Pandas Dataframe which looks like this:
As you can see, I have successfully concatenated the Month and Day columns to get the "Monthdate" column. Now, as I am doing a time series analysis for practice, I would like to add a column for year in the dataframe. However, the data does not have year information, so , I would like to leave it to Python to randomize the years from 2019 - 2021.
Is there a way to do this or I am doomed because there is no data available?
Solution 1:[1]
-next time provide a code sample to work with, please-
Concatenate your string with a random year chosen with random.randint
from random import randint
df['mdy'] = df.Monthdate.apply(lambda val: f'{val} {randint(2019,2022)}')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Anton Frolov |

