Course Content
Analyzing and Visualizing Real-World Data
Analyzing and Visualizing Real-World Data
Processing Dates
We are close to finishing! Let's see what data types our data has now.
# Loading the library import pandas as pd # Reading the data df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/72be5dde-f3e6-4c40-8881-e1d97ae31287/shops_data3.csv') # Checking columns types print(df.dtypes)
As you can see, there is only one object
column left. You may be wondering why this is bad. Actually, we can leave it as is, but pandas
provides us with tools to work with dates and times. Unfortunately, datetime
is not a built-in data type in Python, so we can't convert the column and save it. We will have to convert it after loading every time. To convert to datetime
type, we need to use the .to_datetime()
method of pd
.
# Loading the library import pandas as pd # Reading the data df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/72be5dde-f3e6-4c40-8881-e1d97ae31287/shops_data3.csv') # Displaying first four dates before converting print(df['Date'].head(4)) # Change column type df['Date'] = pd.to_datetime(df['Date']) # Displaying first four dates and dtypes of dataframe print(df['Date'].head(4)) print(df.dtypes)
As you can see, visually the dates have changed, i.e., they are now represented in a different format.
Thanks for your feedback!