0% found this document useful (0 votes)
47 views22 pages

Pandas Programming Examples and Visualizations

Uploaded by

rg6307550360
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views22 pages

Pandas Programming Examples and Visualizations

Uploaded by

rg6307550360
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PROGRAMS BASED ON PANDAS LIBRARY

1)Write a Pandas program to create and display a one-dimensional


array-like object containing an array of data using Pandas module.

CODE:

import pandas pd

ds = [Link]([2,4,6,8,10])

print(ds)

OUTPUT:

2)Write a Pandas program to convert a panda module series to python


list and its type.

CODE:

import pandas as pd

ds = [Link]([2,4,6,8,10])

print(“pandas series and type”)

print(ds)

print(type(ds))

print(“convert pandas series to python list”)

print([Link]())

print(type([Link]()))
OUTPUT:

3)Write a Pandas program to add,subtracr,multiple and divide two


Pandas Series.

Sample Series: [2,4,6,8,10],[1,3,5,7,9]

CODE:

import pandas as pd

ds1 = [Link]([2,4,6,8,10])

ds2 = [Link]([1,3,5,7,9])

ds = ds1 + ds2

print("Add two Series:")

print(ds)

print("Subtract two Series:")

ds = ds1 - ds2

print("Multiply two Series:")

ds = ds1 * ds2

print(ds)

print("Divide Series1 by Series2:")


ds = ds1 / ds2

print(ds)

OUTPUT:

4)write a Pandas program to compare the elements of the two Pandas


Series.

Sample Series:[2,4,6,8,10],[1,3,5,7,10]

CODE:

import pandas as pd

ds1 = [Link]([2,4,6,8,10])

ds2 = [Link]([1,3,5,7,10])

print("Series1:")

print(ds1)
print("Series2:")

print(ds2)

print("Compare the elements of the said Series:")

print("Equals:")

print(ds1==ds2)

print("Greater than:")

print(ds1>ds2)

print("Less than:")

print(ds1<ds2)

OUTPUT:

5)write a Pandas program to convert a NumPy array to Pandas Series.


CODE:

import pandas as pd

import numpy as np

np_array = [Link]([10,20,30,40,50])

print("NumPy Array:")

print(np_array)

new_series = [Link](np_array)

print("converted Pandas series:")

print(new_series)

OUTPUT:

6)Write a Pandas program to add some data to an existing series .

CODE:

import pandas as pd

s = [Link](['100','200','python','300.12','400'])

print("Original Data series:")

print(s)

print("\nData Series after adding some data:")

new_s = [Link]([Link](['500','php']))
print(new_s)

OUTPUT:

7)write a Pandas program to convert a given Series to an array.

CODE:

import pandas as pd

import numpy as np

s1 = [Link](['100','200','python','300.12','400'])

print("Original Data Series:")

print(s1)

print("Series to an array")

a = [Link]([Link]())

print(a)

OUTPUT:
8)Write a Pandas program to change the order pf index of a given
series.

CODE:

import pandas as pd

s = [Link](data = [1,2,3,4,5],index =
['A','B','C','D','E'])

print("Original Data Series:")

print(s)

s = [Link](index = ['A','B','C','D','E'])

print("Data Series after changing the order of index:")

print(s)

OUTPUT:
PROGRAMS USING PANDAS AND MATPLOTLIB

>> PYTHON PANDAS : DATA HANDLING

1)Write a Pandas program to multiply and divide two Pandas Series.

CODE:

d1 = [Link]([2,4,8,10])

d2 = [Link]([1,3,7,9])

print("multiply two series:")

ds = d1 * d2

print(ds)

print("divide series1 by series2:")

ds = d1/d2

print(ds)

OUTPUT:

2)Create a pandas series from dictionary of values and an ndarray.

CODE:

import pandas as pd
import numpy as np

s = [Link]([Link]([1,3,4,7,8,9]))

print(s)

dic = {'X':10,'Y':20,'Z':30}

d = [Link](dic)

print(d)

OUTPUT:

3)Write a code to create series object using dictionary.

CODE:

import pandas as pd

d = {'physical quantity':['vector area element','electric


field'],'symbol':['S','E']}

df = [Link](d,index=[1,2])

print(df)

OUTPUT:
4)Create series object by giving index.

CODE:

import pandas as pd

d2 = [Link]([1,2,3,4],index=['a','b','c','d'])

print(d2)

OUTPUT:

5)Filter rows based on different criteria such as duplicate rows.

CODE:

import pandas as pd

data={'Name':['Aman','Rohit','Deepika','Aman','Deepika','Sohit','
Geeta'],

'Sales':[8500,4500,9200,8500,9200,9600,8400]}

sales=[Link](data)

duplicated = sales[[Link](keep=False)]

print("duplicate Row:\n",duplicated)

OUTPUT:
6) Write a program to print following details.
i) head() function
ii)tail() funt=ction
CODE:
import pandas as pd
d2 = [Link]([1,2,3,4])
print([Link])
OUTPUT:

7)Write a progam to create a DataFrame o store weight , age and names


of 3 people. Print the DataFrame and its transpose.

CODE:

import pandas as pd

df = [Link] ({'weight':[42,75,66],

'name':['ARANV','CHARLES','GURU'],

'AGE':[15,22,35]})

print('Original DataFrame')

print (df)

print('Transpose')

print(df.T)

OUTPUT:
8)Consider the saleDF shown below:

Write a program to rename indexes of ‘zoneB’ and ‘zoneC’ as ‘Central’


and ‘Dakshin’ respectively and the column names ‘Target’ and ‘Sales’
as ‘Targeted’ and ‘Achieved’ respectively.

CODE:

print([Link](index={'zoneB':'Central','zoneC':'Dakshin'},

columns = {'Target':'Targeted','Sales':'Achieved'}))

OUTPUT:
>> PROGRAMS BASED ON DATA VISUALIZATION

1)Given the school result data, analyses the performance of the


students on different parameters.

CODE:

import pandas as pd

import [Link] as plt

subject=['physics','chemistry','mathematics','biology','computer'
]

marks = [80,75,70,78,82]

[Link](subject,marks,'r',marker='*')

[Link]('marks scored')

[Link]('SUBJECT')

[Link]('MARKS')

[Link]()

OUTPUT:
2)Write a program to plot a bar chart in python to display the result
of a school for five consecutive years.

CODE:

import pandas as pd

import [Link] as plt

year = ['2015','2016','2017','2018','2019']

p = [98.50,70,99,90.5,61.50]

j = ['b','g','r','m','c']

[Link](year,p,width=0.2,color=j)

[Link]('year')

[Link]('pass%')

[Link]()

OUTPUT:
3)Plot the following line chart:

 Write a title for the chart “The Weekly Income Report”.


 Write the appropriate titles of both the axes.
 Write code to Display legends.
 Display red color for the line.
 Use the line style – dashed.
 Display diamond style markers on data points.

CODE:

import pandas as pd

import [Link] as pd

day = ['monday','tuesday','wednesday','thursday','friday']

inc = [510,350,475,580,600]

[Link](day,inc,label='Income',color='pink',linestyle='dashed',ma
rker='D')

[Link]('THE WEEKLY INCOME REPORT')

[Link]('days')

[Link]('income')

[Link]()

[Link]()

OUTPUT:
4)Write a program to plot a horizontal bar chart from the heights of
some students.

CODE:

import [Link] as plt

height = [5.1,5.5,6.0,5.0,6.3]

names = ('asma','bela','chris','diya','saqib')

[Link](names,height)

[Link]("HEIGHT")

[Link]("NAMES")

[Link]()

OUTPUT:
5)Val is a list having three inside it. It contains summarised data of
three different trials conducted by company A. Create a bar chart that
plots these three sublists of Val in single chart. Keep thw width of
each bar as 0.25.

CODE:

import [Link] as plt

import numpy as np

Val = [[5.,25.,45.,20.],[4.,23.,49.,17.],[6.,22.,47.,19.]]

X = [Link](4)

[Link](X+0.00,Val[0],color='b',width=0.25)

[Link](X+0.25,Val[1],color='g',width=0.25)

[Link](X+0.50,Val[2],color='r',width=0.25)

[Link]()

OUTPUT:
PROGRAMS BASED ON MYSQL

1)Create a database.

CODE:

mysql> create database items;

OUTPUT:

2)Show the database created.

CODE:

mysql> show databases;

OUTPUT:

3)Use the database items.

CODE:

mysql> use items;

OUTPUT:
4)Create a table name items.

CODE:

mysql> create table items(

mysql> create table item(

-> i_code int,

-> Name varchar(30),

-> Category varchar(30),

-> Rate int,

-> Qty int);

OUTPUT:

5)Insert details of table items.

CODE:

mysql> insert into itemss values

-> (1001,'MASALA DHOSA','SOUTH INDIAN',60,3),

-> (1002,'VADA SAMBHAR','SOUTH INDIAN',40,2),

-> (1003,'IDILI SAMBHAR','SOUTH INDIAN',40,1),

-> (2001,'CHOW MEIN','CHINESE',80,4),

-> (2002,'DIMSUM','CHINESE',60,2),
-> (2003,'SOUP','CHINESE',50,1),

-> (3001,'PIZZA','ITALIAN',240,4),

-> (3002,'PASTA','ITALIAN',125,3);

OUTPUT:

6)Display all the details in a table.

CODE:

mysql> select * from items;

OUTPUT:
7)Display only Name and Category.

CODE:

mysql> select Name,Category from items;

OUTPUT:

8)Display the details whose i_code is 2002.

CODE:

mysql> select * from items

-> where i_code=2002;

OUTPUT:
9)Display Maximum rate of items.

CODE:

mysql> SELECT MIN(RATE)

-> from items;

OUTPUT:

10)Display all the items name in alphabetical order.

CODE:

mysql> select * from items

-> order by Name;

OUTPUT:

You might also like