Business Days with Custom Holidays

  30 May 2019
   python

Importing Packages and Datasets

import pandas as pd
start_date = ['2019-06-03', '2019-06-13', '2019-10-01', '2019-09-01']
end_date =  ['2019-08-31', '2019-06-21', '2019-10-25', '2019-12-25']
data = pd.DataFrame(list(zip(start_date,end_date)), columns = ['Start Date', 'End Date'])
data
Start DateEnd Date
02019-06-032019-08-31
12019-06-132019-06-21
22019-10-012019-10-25
32019-09-012019-12-25

Custm Holidays List

holiday_dates = [pd.datetime(2019, 8, 15), pd.datetime(2019, 10, 2), pd.datetime(2019, 10, 8),
                 pd.datetime(2019, 10, 28), pd.datetime(2019, 12, 25)]

  Applying functions over pandas dataframe using apply, applymap and map

  23 Apr 2019
   python

Importing Packages and Datasets

import pandas as pd
import numpy as np
data = pd.DataFrame(np.random.rand(4, 3)*100,
                    columns=['Physics','Çhemistry','Maths'],
                    index = ['Student 1', 'Student 2','Student 3','Student 4'])
data
PhysicsÇhemistryMaths
Student 159.48894414.88841152.794760
Student 221.87211366.48164687.190572
Student 39.88591954.44967458.696036
Student 433.8043786.28629530.373699

Applymap

RoundUpto2Decimal = lambda x: round(x,2)
data.applymap(RoundUpto2Decimal)
PhysicsÇhemistryMaths
Student 159.4914.8952.79
Student 221.8766.4887.19
Student 39.8954.4558.70
Student 433.806.2930.37

  Indexing and Sorting a dataframe using iloc and loc

  08 Apr 2019
   python

There are multiple ways in pandas by which a dataframe can be indexed i.e, selecting particular set of rows and columns from a dataframe. For a detailed description over this topic, once can refer official pandas documentation - Indexing and Selecting Data

We’ll discuss the following -

Let’s begin with loading a sample dataset and required python packages.

import pandas as pd
import numpy as np
#Fetching data from url as csv by mentioning values of various paramters
data = pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data",
                   header = None,
                   index_col = False,
                   names = ['sepal_length','sepal_width','petal_length','petal_width','iris_class'])
# visualising first five rows of sample dataset (Iris)
data.head()

The sample first five rows of data looks like (can be viewed using data.head())-

sepal_lengthsepal_widthpetal_lengthpetal_widthiris_class
05.13.51.40.2Iris-setosa
14.93.01.40.2Iris-setosa
24.73.21.30.2Iris-setosa
34.63.11.50.2Iris-setosa
45.03.61.40.2Iris-setosa