In this article, we will discuss a way to add a new column in DataFrame with the current datetime in Pandas.
Table Of Contents
Preparing DataSet
First we will create a DataFrame from list of tuples i.e.
import pandas as pd # List of Tuples employees= [('Mark', 'US', 'Tech', 5), ('Riti', 'India', 'Tech' , 7), ('Shanky', 'India', 'PMO' , 2), ('Shreya', 'India', 'Design' , 2), ('Aadi', 'US', 'Tech', 11), ('Sim', 'US', 'Tech', 4)] # Create a DataFrame object from list of tuples df = pd.DataFrame(employees, columns=['Name', 'Location', 'Team', 'Experience']) print(df)
Output:
0 Mark US Tech 5 1 Riti India Tech 7 2 Shanky India PMO 2 3 Shreya India Design 2 4 Aadi US Tech 11 5 Sim US Tech 4
Now, suppose we want to add a new column in this DataFrame ‘Last_Login_Time’, and this column should contain current DateTime. Let’s see how to do that.
Add Column with current datetime in Pandas
Pandas provides a new class Timestamp
, and we can call its constructor with value ‘now’. It will give us a datetime64
object, containing the current date and time. To create a new column where all values should contain the current datetime, assign pd.Timestamp('now')
to the [] operator of DataFrame i.e.
# Add new Column with current datetime in Pandas df['Last_Login_Time'] = pd.Timestamp('now') print(df)
Output
Frequently Asked:
Name Location Team Experience Last_Login_Time 0 Mark US Tech 5 2023-01-06 11:09:26.326084 1 Riti India Tech 7 2023-01-06 11:09:26.326084 2 Shanky India PMO 2 2023-01-06 11:09:26.326084 3 Shreya India Design 2 2023-01-06 11:09:26.326084 4 Aadi US Tech 11 2023-01-06 11:09:26.326084 5 Sim US Tech 4 2023-01-06 11:09:26.326084
It added a new column Last_Login_Time
in the DataFrame, and each value of this new column is the current Date Time.
Add Column with current datetime with custom format in Pandas
To add a new column of current datetime in custom format, we can call the strftime()
function on datetime object with custom format. For example,
df['Last_Logout_Time'] = pd.Timestamp('now').strftime("%Y/%m/%d %H:%M:%S:%f") print(df) df['Last_checkout_Time'] = pd.Timestamp('now').strftime("%y-%m-%d %H:%M:%S") print(df)
Output:
Name Location Team Experience Last_Login_Time Last_Logout_Time 0 Mark US Tech 5 2023-01-06 11:15:20.811131 2023/01/06 11:15:20:836950 1 Riti India Tech 7 2023-01-06 11:15:20.811131 2023/01/06 11:15:20:836950 2 Shanky India PMO 2 2023-01-06 11:15:20.811131 2023/01/06 11:15:20:836950 3 Shreya India Design 2 2023-01-06 11:15:20.811131 2023/01/06 11:15:20:836950 4 Aadi US Tech 11 2023-01-06 11:15:20.811131 2023/01/06 11:15:20:836950 5 Sim US Tech 4 2023-01-06 11:15:20.811131 2023/01/06 11:15:20:836950 Name Location Team Experience Last_Login_Time Last_Logout_Time Last_checkout_Time 0 Mark US Tech 5 2023-01-06 11:15:20.811131 2023/01/06 11:15:20:836950 23-01-06 11:15:20 1 Riti India Tech 7 2023-01-06 11:15:20.811131 2023/01/06 11:15:20:836950 23-01-06 11:15:20 2 Shanky India PMO 2 2023-01-06 11:15:20.811131 2023/01/06 11:15:20:836950 23-01-06 11:15:20 3 Shreya India Design 2 2023-01-06 11:15:20.811131 2023/01/06 11:15:20:836950 23-01-06 11:15:20 4 Aadi US Tech 11 2023-01-06 11:15:20.811131 2023/01/06 11:15:20:836950 23-01-06 11:15:20 5 Sim US Tech 4 2023-01-06 11:15:20.811131 2023/01/06 11:15:20:836950 23-01-06 11:15:20
It added new columns with current datetime, but in different format.
Summary
Today we learned about different ways to add a new column in dataFrame with current datetime in Pandas. Thanks.