How to Create a Timestamp From Data in Python Dataframe

To create a timestamp from data in a Python dataframe, you can use the to_datetime() function from the pandas library.

The following example shows how to create a timestamp from data in a Python dataframe using the to_datetime() function.

Using to_datetime() Function

We can use the to_datetime() function to create a timestamp from data in a dataframe.

Suppose we have the following dataframe:

# Import pandas library
import pandas as pd

# Create dataframe
office_stuff = pd.DataFrame({
    'Date': ['01-03-2023', '01-03-2023', '01-03-2023', '01-03-2023', '02-03-2023', '02-03-2023'],
    'Product_Code': ['A-101', 'A-102', 'A-103', 'B-101', 'B-102', 'B-104'],
    'Product_Name': ['Laptop', 'Mobile', 'Printer', 'Keyboard', 'Scanner', 'Mouse'],
    'Price': [4500, 550, 250, 50, 350, 50],
    'Status': [1, 1, 1, 0, 1, 1]
})

# Convert date column to timestamp
office_stuff['Date'] = pd.to_datetime(office_stuff['Date'], format='%d-%m-%Y')

# Show updated dataframe
print(office_stuff)

Output: 👇️

        Date Product_Code Product_Name  Price  Status
0 2023-03-01        A-101       Laptop   4500       1
1 2023-03-01        A-102       Mobile    550       1
2 2023-03-01        A-103      Printer    250       1
3 2023-03-01        B-101     Keyboard     50       0
4 2023-03-02        B-102      Scanner    350       1
5 2023-03-02        B-104        Mouse     50       1

In this example, we use the to_datetime() function to convert the Date column in the dataframe office_stuff to a timestamp.

The output shows the updated dataframe with the Date column converted to timestamps.

Conclusion

We can use the to_datetime() function from the pandas library to create a timestamp from data in a Python dataframe.