Thursday 21 September 2023

Pandas: Dataframes: Hello World application

I am using the dataset from https://www.kaggle.com/datasets/patrickb1912/ipl-complete-dataset-20082020, to demonstrate the hello world application.

 

Project hierarchy looks like below.

data
|____csv
| |____ipl_matches.csv
src
|____csv
| |____hello_world.py

 

As you see above snippet, there are two folders

a.   data : to hold the sample data sets

b.   src : to hold the panda programs.

 

Follow step-by-step procedure helps to understand the hello world program in Pandas.

 

Step 1: Create a file hello_world.py in the project src/csv directory.

 

Step 2: Import the pandas Library.

import pandas as pd

 

Step 3: Specify the file path to the CSV file relative to the project directory.

file_path = '../../data/csv/ipl_matches.csv'

 

Step 4: Set the maximum number of columns to display as unlimited.

pd.set_option('display.max_columns', None)

 

Step 5: Read the CSV file into a DataFrame.

df = pd.read_csv(file_path)

 

Step 6: Print the dataframe data to console. For example, below snippet print first 5 rows and 4 columns of the dataframe.

 

print(df.iloc[:5, :4] )

 

Find the below working application.

 

hello_world.py

# Import the pandas Library.
import pandas as pd

# Specify the file path to the CSV file relative to the project directory.
file_path = '../../data/csv/ipl_matches.csv'

# Set the maximum number of columns to display as unlimited
pd.set_option('display.max_columns', None)

# Read the CSV file into a DataFrame
df = pd.read_csv(file_path)

print(df.iloc[:5, :4] )

 

Output

       id        city        date player_of_match
0  335982   Bangalore  2008-04-18     BB McCullum
1  335983  Chandigarh  2008-04-19      MEK Hussey
2  335984       Delhi  2008-04-19     MF Maharoof
3  335985      Mumbai  2008-04-20      MV Boucher
4  335986     Kolkata  2008-04-20       DJ Hussey

 

Total project structure looks like below.


 

 

 

 

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment