Basic Operations in Python

Introduction to Python:


Python is a high level, interpreted, interactive language. In this tutorial, you will learn basic operations in Python3. To get the data file that I used in this tutorial, click here.

1. Check Current Working Directory

import os
os.getcwd( )

Through os.getcwd( ) you will get the current directory. To change the directory you have to use the following command:

os.chdir(” “)

In Parenthesis, you have to copy your location of the data file followed by /

2. Import Libraries
To Load your data file, you have to use pandas library, if it is not installed in your jupyter notebook then you can use the following command:

!pip install pandas

To import the library, use the following commands:

import pandas as pd

3. Load data file in Python
To Load the data file, there is function in pandas read_csv(for CSV File), if you have data file other than CSV, then go to this article. To load the data file, then use the following command:

df = pd.read_csv(“”)

4. Getting the column names of the dataset

df.columns

5. Getting the structure of the dataset

type(df)

6. Getting the number of variables and observation in the dataset

df.shape

7. Getting the first 10 rows

df.head(10)

8. Getting the last 10 rows

df.tail(10)

9. Getting the first 10 rows using iloc

df.iloc[:10]

10. Unique values in a column

df[‘CarName’].unique( )

11. Count of unique values in a column

df[‘CarName’].nunique( )

12. Distribution of unique values in a column

df[‘CarName’].value_counts( )

13. Summary of a variable

df[‘CarName’].describe( )

14. Calculate Statistical Quantities

df[‘car_ID’].mean( )
df[‘car_ID’].median( )
df[‘car_ID’].var( )
df[‘car_ID’].std( )

15. Add all the values of column

df[‘car_ID’].sum( )

Leave a Reply

Your email address will not be published. Required fields are marked *