To shift a column in a pandas DataFrame one day forward, you can use the shift()
method. Here's how you can do it:
main.py265 chars11 lines
Output:
main.py120 chars7 lines
In the code above, we first import the pandas library. We then create a sample DataFrame with a 'date' column and a 'value' column. We use the shift()
method on the 'date' column, specifying a shift of 1 day forward using the freq
parameter set to 'D'. Finally, we print the resulting DataFrame with the shifted 'date' column.
Note: The shift()
method returns a new DataFrame with the shifted column, so we assign it back to the original column to update the DataFrame in-place.
gistlibby LogSnag