Add a column in Pandas DataFrame with current Date

In this article, we will discuss a way to add a new column in DataFrame with the current date 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_Visited_date’, and this column should contain current date only. Let’s see how to do that.

Add Column with current Date in Pandas

Pandas provides a new class Timestamp, and we can call its constructor with a string value now. It returns a datetime64 object, containing the current date and time. Then we can call the date() function of this object, to get the current date only. To create a new column with current date only, assign pd.Timestamp('now').date() to the [] operator of DataFrame i.e.

## Add column with current date
df['Last_Visited_Date'] = pd.Timestamp('now').date()

print(df)

Output

     Name Location    Team  Experience Last_Visited_Date
0    Mark       US    Tech           5        2023-01-06
1    Riti    India    Tech           7        2023-01-06
2  Shanky    India     PMO           2        2023-01-06
3  Shreya    India  Design           2        2023-01-06
4    Aadi       US    Tech          11        2023-01-06
5     Sim       US    Tech           4        2023-01-06

It added a new column Last_Visited_Date with current date only.

We can also change the format of date while adding the new column. For example, let’s add a new column with current date but the format of Date will be ‘DD/MM/YYYY’. Let’s see the example,

## Add column with current date
df['Last_Login_Date'] = pd.Timestamp('now').date().strftime("%d/%m/%Y")

print(df)

Output:

     Name Location    Team  Experience Last_Login_Date
0    Mark       US    Tech           5      06/01/2023
1    Riti    India    Tech           7      06/01/2023
2  Shanky    India     PMO           2      06/01/2023
3  Shreya    India  Design           2      06/01/2023
4    Aadi       US    Tech          11      06/01/2023
5     Sim       US    Tech           4      06/01/2023

It added a new column Last_Login_Date with current date only but in format ‘DD/MM/YYYY’.

Summary

Today we learned about different ways to add a new column in dataFrame with current date only in Pandas. Thanks.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top