Machine Learning Tutorial-Pandas

How to work with data frame using Python

Import Library for Data frame

Pandas :

  1. pandas is an open source library providing high-performance, easy-to-use data structures and data analysis tools
    for the python programming language.
  2. initially developed by wes McKinney in 2008 at AQE capital Management and later it was open-sourced
  3. Now , it is has become a famous library usedby data scientists all over the world.

Working with pandas:
installing pandas in python

        pip install pandas
        !pip install pandas

       # this command will install the necessory packages presenet in panadas.

Working with pandas:


1.Dataframe
Data frames are defined as two-dimensional labelled data structures with columns of potentially different types

    #importing pandas library

        import pandas as pd

working with pandas
#preparing a data frames using python dictionary and printing data

data={' Name':['Santosh','Shivani','EKTA','jyoti','RAJU','AMIT'],
'AGE':[21,19,22,39,29,20],
'salary':[90000,40000,4000003,343400,343400,40000]
}
obj=pd.DataFrame(data)
print(obj)

working with pandas
#printing a specific columns of dataprint

    print(obj['AGE'])
    print(obj.AGE)

#printing columns of data
    print(obj.columns)

working with pandas
#printing the data in a 2-D array
print(obj.values)

#drop a specific row
print(obj.drop(1))

#drop a specific columns
print(obj.drop('Age',axis=1))
#axis=1 for columns
#axis=0 for rows

2.Indexing and Selection

    #indexing your data with rows 
        data=pd.DataFrame(data,index=['Shivani'])
        print(data)

    #indexing format with columns
        print(data[['Age','Name']])

3.Descriptive statistic

printing multiple statistic about data
    print(data.describe())

    #printing mean of salaries columns 
        print(data.loc[:,'Salary'].mean())
    #printing minimum age of an employee
        print(data.loc[:,'AGE'].min())

4. Reading and writing files

    #reading and printing the data
    data=pd.read_csv('FIlePath')
    print(data.head())

    csv===extension
    csv=comma seperated value

    #writing a file 

    pd.DataFrame(data).to_csv('FilePathWithFilName')

”’Read ClipBoard”’
# This method is very similar to the read_csv method of pandas or read_table but where the data comes
#from clipboard buffer instead of a CSV file.
# First, you need to have text from data frames. It’s important to have a text structured in a data frame way
#with data order in row and columns.
# After that, you need to copy the data using ctrl+c
–copy Dictionary
Press Ctrl+c

execute pd.read_clipboard()

Read Excel and HTML file
# reads data table from a excel file
pd.read_excel(‘FILE PATH’)

# reads data table from a html file
    pd.read_html('File Path')

Leave a Comment

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

Scroll to Top