I have a dataframe with a Date
column that has datetime objects in it. I can extract the month and year from the Date
column and store them in columns of the respective names using the code below.
df["Month"] = df["Date"].dt.month
df["Year"] = df["Date"].dt.year
But is there a way to do this in just one line?
CodePudding user response:
Here's how you can do it:
df['Month'], df['Year'] = df["Date"].dt.month, df["Date"].dt.year