Working with Pandas - The Basics

Make sure you have a sample data to work with. Now open your favourite IDE.

Let's use jupyter notebook for this tutorial. In the terminal run;

pip install jupyterlab

Once the installation is complete. Open jupyter notebook with the below command;

jupyter notebook

 With pandas installed in your project's virtual environment, let's import pandas so we can use it inside our project (if not pip install pandas using 'terminal'. Check above for instructions);

import pandas as pd

Now let's  create a data frame to view our CSV data file. So the below code simply creates a data frame using 'pd' to read the CSV file and we placed it into the variable df and opened df (df).

df = pd.read_csv('sample_data')
df

Now some basic Pandas Attributes;

df.shape

1. Shows you the number of rows and columns (rows, columns) E.g. (5, 10). i.e. 5 rows and 10 columns in the dataframe

df.describe

2. Returns columns and column data in a snippet. Not in rows and columns

df.head()

3. Returns the first 5 rows in the dataframe and all columns

df.loc

4. Returns data from a row number or with a column 'name. 

df.loc[5500, 'City']

E.g. The above returns the row indexed; 5500 under column name 'City'

df.iloc

5. iloc returns data from rows and columns by integer inputs only. 

E.g. The dataframe below will return row 5 and column 5's data.

df.iloc[5, 5]

If you need assistance with your projects feel free to email me at info@airgad.com or whatsapp Jesse stay safe!