Pandas DataFrame: Convert the column type from string to datetime format

Pandas DataFrame: Convert the column type from string to datetime format

While working with data in Pandas, it is not an unusual thing to encounter time series data and we know Pandas is a very useful tool for working with time series data in Python.

Let’s see how we can convert a DataFrame column of strings (in dd/mm/yyyy format) to datetime format. We cannot perform any time series based operation on the dates if they are not in the right format. In order to be able to work with it, we are required to convert the dates into the datetime format.

Convert Pandas DataFrame column type from string to datetime format using pd.to_datetime() function.

#importing pandas as pd 
import pandas as pd 
  
# Creating the dataframe 
d = pd.DataFrame({'Date':['11/8/2011', '04/23/2008', '10/2/2019'], 
                'Event':['Music', 'Poetry', 'Theatre'], 
                'Cost':[10000, 5000, 15000]}) 

d
No alt text provided for this image
d.info()
No alt text provided for this image
d['Date']= pd.to_datetime(d['Date']) 

d.info()
No alt text provided for this image
print(d.Date[0].day)
print(d.Date[0].month)
print(d.Date[0].year)
No alt text provided for this image

Follow Me on Twitter and LinkedIn









要查看或添加评论,请登录

Joseph Sefara的更多文章

  • Self-Study Data Science

    Self-Study Data Science

    As a data science consultant, lots of people interested in getting into data science have contacted me for guidance on…

    2 条评论
  • 162+ Data Science Interview Questions

    162+ Data Science Interview Questions

    A typical interview process for a data science position includes multiple rounds. Often, one of such rounds covers…

  • Importance of Data Normalisation for Data Science and Machine Learning Models

    Importance of Data Normalisation for Data Science and Machine Learning Models

    Normalisation is a technique often applied as part of data preparation for machine learning. The goal of normalisation…

  • Using Data Science to Know if the Customers will Buy the Products in their Cart or not?

    Using Data Science to Know if the Customers will Buy the Products in their Cart or not?

    Using Machine Learning (ML) Classifier specifically XGBoost to predict if a customer will eventually make a purchase…

    2 条评论
  • SVMs versus Logistic Regression

    SVMs versus Logistic Regression

    Like logistic regression (LR), support vector machines (SVMs) can also be generalised to categorical output variables…

    1 条评论
  • When to Scale, Standardise, or Normalise with Scikit-Learn

    When to Scale, Standardise, or Normalise with Scikit-Learn

    Many machine learning algorithms work better when features are on a relatively similar scale and close to normal…

    1 条评论
  • Logistic Regression with Keras

    Logistic Regression with Keras

    Logistic Regression (LR) is a simple yet quite effective method for carrying out binary classification tasks. There are…

  • Advice to Recent Graduates: Plan, Negotiate and Network

    Advice to Recent Graduates: Plan, Negotiate and Network

    It has been more than 20 years since I graduated college. Since then my career has been productive and focused around…

  • Respect and Love Your Elders

    Respect and Love Your Elders

    Wise people are very few, if we put a habit of hearing then wise people from our home to the outside world will find…

社区洞察

其他会员也浏览了