Load, clean and explore data with Python Pandas

In machine learning, we need to be able to manipulate our data. And pandas is a good library for this task. To manipulate is to load, clean and explore our data aka Data Exploratory Analysis (EDA). This step is important in machine learning.

Load data

import pandas as pd 
dataset = pd.read_csv(r'C:\Users\CS\Desktop\FX\MajorExchangeRates.csv') 

Actual data is 2763 rows and due to space constraint only 10 rows are displayed. This data contained continous features.


Clean data

After loading, cleaning your data is a must.

Explore data

You can choose to display certain columns or rows.

#display 1st 5 rows of data by default.
print (dataset.head()) 

#you can input number of rows you want as an argument for the method.
#display 1st 12 rows of data.
print (dataset.head(12)) 

#display only USD, JPY and THB columns
print (dataset[['USD', 'JPY', 'THB']])

#display rows by row index. Print all columns at index 3(ie. 4th row)
print (dataset.loc[3 , :])
#will display in this format.. see below.

Date    11/10/2019
USD         1.1043
JPY         119.75
GBP        0.87518
CHF         1.1025
AUD         1.6246
CNY         7.8417
IDR        15601.5
INR        78.4875
MYR          4.622
SGD         1.5177
THB         33.642
Name: 3, dtype: object

Display columns and index names. The index is normally running integers therefore not much of a use.

index = dataset.index
column = dataset.columns

To remove certain columns from dataset. Inplace = True means the original dataset table will be replaced by this new table which the 2 columns are dropped. Put inplace = False if you want to give the new table another name.

#choose categories to drop
toDrop = ['JPY' , 'MYR']
# axis=1 means remove colum and inplace=True means dataset file will be overwritten.
dataset.drop(toDrop, axis = 1, inplace = True)
print (dataset)