1 of 5

https://colab.research.google.com/drive/19EuG9iFue70wNctnWxZFERZAfJxx40_4?authuser=0#scrollTo=hitfm1FBLqjO

Colab link:

2 of 5

  • Dataset:
          • In Python, a dataset is a collection of data that can be used for machine learning, data analysis, and other tasks
          • Datasets can be stored in a variety of formats, such as CSV file,XLS.
  • Csv file:

A CSV file (Comma Separated Values file) is a type of plain text file that uses specific structuring to arrange tabular data.

        • The text inside a csv file is laid out in row, and each of those has column ,all separated by commas which is used to define and separate cells.

Pandas:

Pandas is an open-source Python library that provides high performance, easy-to-use data structure, and data analysis tools for the Python programming language.

3 of 5

#Importing pandas library

  • import pandas as pd

#Creating DataFrame

  • data= {'name': ['akshat', 'shivam', 'rohit'], 'roll_no': [10, 12, 15], 'marks':[7, 8, 9]}
  • df= pd.DataFrame(data)
  • #turning a dataframe in csv file
  • df.to_csv('student.csv')
  • cv = pd.read_csv('student.csv')
  • Cv
  • #Removing index
  • df.to_csv('student.csv', index=False)
  • cv = pd.read_csv('student.csv')
  • Cv
  • #customizing header
  • new_column=['fathers_name','marks','roll_no']
  • df.to_csv('student.csv', index=False, header=new_column)
  • cv=pd.read_csv('student.csv')
  • Cv

  • #Handling missing value
  • data1={'name':['faisal_iqbal', 'sadia_zaib', 'afia'], 'hometown':['wb', 'mp', pd.NA]}
  • df=pd.DataFrame(data1)
  • df.to_csv('student.csv', index=False, na_rep='unknown')
  • cv=pd.read_csv('student.csv')
  • cv

Reading and writing in csv file

4 of 5

  • #Skipping Rows While Reading CSV
  • cv=pd.read_csv('student.csv', names=new_col, skiprows=[0])

  • cv=pd.read_csv('student.csv', names=new_col, skiprows=[0,2])
  • #Reading top element
  • cv=pd.read_csv('student.csv')
  • cv.head(2)

#Seeing from bottom

  • cv.tail(2)

Original Top 2 Element

Skipping 0 and 1

Bottom 2 element

5 of 5

  • #delete (drop) if any of the columns is null
  • data1={'name':['faisal_iqbal', 'sadia_zaib', 'afia'], 'hometown':['wb', 'mp', pd.NA]}
  • df=pd.DataFrame(data1)
  • df.to_csv('student.csv')
  • cv= pd.read_csv('student.csv')
  • cv.dropna(inplace=True)
  • Cv
  • #filtering the data
  • data= {'name': ['akshat', 'shivam', 'rohit'], 'roll_no': [10, 12, 15], 'marks':[7, 8, 9]}
  • df= pd.DataFrame(data)
  • df.to_csv('student.csv')
  • cv=pd.read_csv('student.csv')
  • cv=cv.query('marks>7')
  • cv
  • #filtering the string
  • df = pd.read_csv('student.csv')
  • df = df[['name', 'marks']]
  • df = df[df['name'].isin(['shivam', 'rahul'])]
  • df.to_csv('filtered_data.csv')
  • cv1=pd.read_csv('filtered_data.csv')
  • cv1