Populate current datetime in pandas python

Populate current datetime in pandas python can be done by using to_datetime() function. Let’s see how to

  •  Populate current datetime in pandas python
  •  Populate current datetime with string format in pandas python

First let’s create a dataframe


import pandas as pd
import numpy as np

df1 = {
    'State':['Arizona AZ','Georgia GG','Newyork NY','Indiana IN','Florida FL'],
   'Score1':[4,47,55,74,31]}

df1 = pd.DataFrame(df1,columns=['State','Score1'])
print(df1)

df1 will be

Populate current datetime in pandas python 1

 

Populate current datetime in pandas python:

Populating current datetime in pandas can be done by using to_datetime() function with “now” argument as shown below.


df1['date_time'] = pd.to_datetime('now')
print(df1)

so the resultant dataframe will be

Populate current datetime in pandas python 2

 

Populate current datetime in python pandas with string format:

Populating current datetime in pandas with string format can be done by using to_datetime() function and strftime() function as shown below.

df1['date_time'] = pd.to_datetime('now').strftime("%Y-%m-%d %H:%M:%S")
print(df1)

So the resultant dataframe has date_time column in string format

Populate current datetime in pandas python 2

p Populate current datetime in pandas python                                                                                                         n Populate current datetime in pandas python

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    View all posts