8. Datetime

file-download
57KB
  1. Load the data and display.

chevron-rightSolutionhashtag
import pandas as pd
df=pd.read_csv('bakery_sales.csv')
df

# Output
        datetime		total
0	11-07-2019 15:35	23800.0
1	11-07-2019 16:10	15800.0
2	12-07-2019 11:49	58000.0
3	13-07-2019 13:19	14800.0
4	13-07-2019 13:22	15600.0
...	...	...	...
2415	02-05-2020 11:37	19500.0
2416	02-05-2020 11:39	19800.0
2417	02-05-2020 12:15	14300.0
2418	02-05-2020 13:45	15000.0
2419	02-05-2020 14:45	24100.0
2420 rows × 3 columns
  1. Convert the datetime column into proper datetime format, considering the data in the column is in (dd-mm-yyyy hh:mm) format.

chevron-rightSolutionhashtag
df['datetime']=pd.to_datetime(df['datetime'],format="%d-%m-%Y %H:%M")
df

# Output

        datetime		total
0	2019-07-11 15:35:00	23800.0
1	2019-07-11 16:10:00	15800.0
2	2019-07-12 11:49:00	58000.0
3	2019-07-13 13:19:00	14800.0
4	2019-07-13 13:22:00	15600.0
...	...	...	...
2415	2020-05-02 11:37:00	19500.0
2416	2020-05-02 11:39:00	19800.0
2417	2020-05-02 12:15:00	14300.0
2418	2020-05-02 13:45:00	15000.0
2419	2020-05-02 14:45:00	24100.0
2420 rows × 3 columns
  1. Separate day, month name, and year in different columns from datetime.

chevron-rightSolutionhashtag
df['date']=df['datetime'].dt.date
df['time']=df['datetime'].dt.time
df

# Output
        datetime		total	date	        time
0	2019-07-11 15:35:00	23800.0	2019-07-11	15:35:00
1	2019-07-11 16:10:00	15800.0	2019-07-11	16:10:00
2	2019-07-12 11:49:00	58000.0	2019-07-12	11:49:00
3	2019-07-13 13:19:00	14800.0	2019-07-13	13:19:00
4	2019-07-13 13:22:00	15600.0	2019-07-13	13:22:00
...	...	...	...	...	...
2415	2020-05-02 11:37:00	19500.0	2020-05-02	11:37:00
2416	2020-05-02 11:39:00	19800.0	2020-05-02	11:39:00
2417	2020-05-02 12:15:00	14300.0	2020-05-02	12:15:00
2418	2020-05-02 13:45:00	15000.0	2020-05-02	13:45:00
2419	2020-05-02 14:45:00	24100.0	2020-05-02	14:45:00
  1. Separate date, month and year from date column.

chevron-rightSolutionhashtag
  1. Extract time from the datetime column.

chevron-rightSolutionhashtag
  1. Create a sample of 700 rows from the dataset and find the oldest date in the dataset.

chevron-rightSolutionhashtag
  1. Create a sample of 700 rows from the dataset and find the latest date in the dataset.

chevron-rightSolutionhashtag
  1. Create a sample of 700 rows from the dataset and arrange them in ascending order on behalf of date.

chevron-rightSolutionhashtag
  1. Display the data of the sale that happened between 1st Aug 2019 and 01 Dec 2019.

chevron-rightSolutionhashtag
  1. Find out the total sales on each date.

chevron-rightSolutionhashtag
  1. Find out total sale on each day.

chevron-rightSolutionhashtag
  1. Find rows where the transaction happened in the afternoon (12 PM - 6 PM)

chevron-rightSolutionhashtag
  1. Find the sale happened on weekends(sat and sun).

chevron-rightSolutionhashtag

Last updated