Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
21 views

Assignments IP Class 12

The document provides instructions on various pandas operations for series and dataframes. It covers creating and manipulating series and dataframes, applying functions like head(), tail(), count(), max(), and exploring attributes like ndim, size, shape, index, values, and dtype. It also demonstrates arithmetic operations on series, adding/deleting columns and rows from dataframes, and importing/exporting data to CSV files.

Uploaded by

Vidyanshu kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Assignments IP Class 12

The document provides instructions on various pandas operations for series and dataframes. It covers creating and manipulating series and dataframes, applying functions like head(), tail(), count(), max(), and exploring attributes like ndim, size, shape, index, values, and dtype. It also demonstrates arithmetic operations on series, adding/deleting columns and rows from dataframes, and importing/exporting data to CSV files.

Uploaded by

Vidyanshu kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Assignment-

Create Series

1. Create a series from dictionary


import pandas as pd
import numpy as np
data = {'a' : 0., 'b' : 1., 'c' : 2.}
s = pd.Series(data,index=['b','c','d','a'])
print(s)

output:

2. create a series from arange function of ndarray/numpy


import pandas as pd
import numpy as np
array = np.arange(1,15,3)
s = pd.Series(array)

Output:

----------------------------------------------

Assignment-
Series Functions/Methods

Suppose a series,
import pandas as pd
>>> s1 = pd.Series([11,22,33,None,44,55],['A','B',None,'D','E','F'])

#output should be at blank side


Name Description Example Output
head(n) Return first n indices s1.head(5) A 11.0
and element B 22.0
NaN 33.0
D NaN
head() Default first five E 44.0
dtype: float64
tail(n) Return last n indices s1.tail(3) D NaN
and element E 44.0
F 55.0
tail() Default last five dtype: float64
count() Return to number of s1.count() 5
items excluded None/NaN
drop() Remove item by index s1.drop('E') A 11.0
B 22.0
NaN 33.0
D NaN
F 55.0
dtype: float64

Assignment-
Series Attributes

Attrib Example Output


utes description
index Return all s1.index Index(['A', 'B', 'C', 'D', 'E',
indices of 'F'], dtype='object')
series
values Returns all s1.values array([11., 22., 33., nan, 44.,
values of 55.])
series
shape Return the s1.shape (6,)
shape of
series
ndim Return s1.ndim 1
number of
dimensions
size Return s1.size 6
total item
including
NaN(None)
empty Return True s1.empty False
if series
is empty
name assign name s1.name=’hi’ hi
to series print(s1.name)
index.name assigns a
name to the
index of
the series

--------------------------------------------------------------------
-------------------------------------------------------------
Assignment-
Arithmetic Operation on panada Series:
Suppose four series s1,s2,s3 and s4

import pandas as pd
>>> s1 = pd.Series([11,22,33,44])
>>> s2 = pd.Series([111,222,333,444])
>>> s3 = pd.Series([11,22,33,44,55,66,77])
>>> s4 = pd.Series([111,222,333,444],index=[5,6,7,8])

>>> s1+s2
Output:
0 122
1 244
2 366
3 488
dtype: int64

OR

>>> s1.add(s2)
Output:
0 122
1 244
2 366
3 488
dtype: int64

# None matching index value get NaN


>>> s1+s3
Output:
0 22.0
1 44.0
2 66.0
3 88.0
4 NaN
5 NaN
6 NaN
dtype: float64

---------------------------------------------------------

Assignment
Create DataFrame
1. Create a DataFrame to display total number of students
from class 9 to 12 of each section

import pandas as pd
ix= [55,59,54,57]
x = [54,55,59,60]
xi= [51,59,63,61]
xii= [52,62,57,50]

col = ['A','B','c','D']
ind = ['IX','X','XI','XII']

df=pd.DataFrame([ix,x,xi,xii],columns=col,index=ind)
print(df)

Output:
A B c D
IX 55 59 54 57
X 54 55 59 60
XI 51 59 63 61
XII 52 62 57 50
2. Create DataFrame from Dictionary of List
import pandas as pd
friends = { 'name': ['Ravi','Kavi','Chavi'],
'age': [38,34,37],
'cont': [123,234,345]
}
df = pd.DataFrame(friends)
print(df)
Output:
name age cont
0 Ravi 38 123
1 Kavi 34 234
2 Chavi 37 345
------------------------------------------------------------------
Assignment-
DataFrame Functions

head() tail() count() max()


import pandas as pd
friends = {
'name':
['Ravi','Kavi','Chavi','Pavi','Amit','Sumit'],
'bst' : [78,65,None,75,63,65],
'eco' : [78,74,75,75,76,67],
'acc' : [66,78,85,65,66,61]
}
df = pd.DataFrame(friends)
1. head()

>>> print(df.head(3)) #gives first three rows

Output:
name bst eco acc
0 Ravi 78.0 78 66
1 Kavi 65.0 74 78
2 Chavi NaN 75 85

>>> print(df.head(n=4))

Output: #gives first four rows


name bst eco acc
0 Ravi 78.0 78 66
1 Kavi 65.0 74 78
2 Chavi NaN 75 85
3 Pavi 75.0 75 65

>>> print(df.head()) # default gives first five rows

Output:
name bst eco acc
0 Ravi 78.0 78 66
1 Kavi 65.0 74 78
2 Chavi NaN 75 85
3 Pavi 75.0 75 65
4 Amit 63.0 76 66

2. tail() # do yourself

3. count()

>>> print(df.count())
Output:
name 6
bst 5
eco 6
acc 6
dtype: int64

4. max() # return maximum value of each


columns

>>> print(df.count())

Output:
name Sumit
bst 78
eco 78
acc 85
dtype: object
-----------------------------------------------------------------

Assignment-

DataFrame attributes

ndim size shape index values dtype

1. ndim # gives no of dimensions

>>> print(df.ndim)
Output:
2
2. size # gives size(rows*columns) of Dat
>>> print(df.size)

Output:
24

3. shape # return (total rows, total columns)

>>> print(df.shape)

Output:
(6,4)

4. index # return index of DataFrame

Output:
RangeIndex(start=0, stop=6, step=1)

5. values # return all values of DataFrame

>>> print(df.values)
Output:
[['Ravi' 78.0 78 66]
['Kavi' 65.0 74 78]
['Chavi' nan 75 85]
['Pavi' 75.0 75 65]
['Amit' 63.0 76 66]
['Sumit' 65.0 67 61]]
6. dtype

---------------------------------------------------

Assignment-
Add Column and row in DataFrame
import panda as pd
friends = {
'name': ['Ravi','Kavi','Chavi'],
'bst' : [68,73,66],
'eco' : [78,74,75],
'acc' : [78,67,66]
}
df = pd.DataFrame(friends)

1. Add column in DataFrame:

>>> df['eng'] = [65,61,69]


print(df)

Output:
name bst eco acc eng
0 Ravi 68 78 78 65
1 Kavi 73 74 67 61
2 Chavi 66 75 66 69

2. Add/Insert row
>>> row = {'Name':'Pavi','bst':67,'Accountancy':78, 'eng': 70}
df = df.append(row,ignore_index=True)
print(df)

Output:
Name bst Accountancy eng
0 Ravi 68 78 65
1 Kavi 73 67 61
2 Chavi 66 66 69
3 Pavi 67 78 70
-------------------------------------------------------------------

Assignment-
Delete Column and Row

import panda as pd
friends = {
'name': ['Ravi','Kavi','Chavi'],
'bst' : [68,73,66],
'eco' : [78,74,75],
'acc' : [78,67,66]
}

1. Delete row:
>>> df = df.drop(1)
print(df)

Output:
name age cont Blood_Gp
0 Ravi 38 123 B+
2 Chavi 37 345 AB+

2. Delete row
df.pop('age')
print(df)

output:

name cont Blood_Gp


0 Ravi 123 B+
2 Chavi 345 AB+

df=df.drop('cont',axis=1)
print(df)

output:
name Blood_Gp
0 Ravi B+
2 Chavi AB+

--------------------------------------------------

Assignment-

Import and export CSV

1. Create/export CSV-file from DataFrame

import pandas as pd
friends = { 'name': ['Ravi','Kavi','Chavi'],
'age': [38,34,37],
'cont': [123,234,345],
'Blood_Gp':['B+','A+','AB+']

}
df = pd.DataFrame(friends)

df.to_csv('H:/My Drive/Project/friends.csv')

output of CSV

name age cont Blood_Gp


0 Ravi 38 123 B+
1 Kavi 34 234 A+
2 Chavi 37 345 AB+
2. Create/Import DataFrame from CSV file
import pandas as pd

df=pd.read_csv('H:/My Drive/Project/friends.csv')
print(df)

Output:
name age cont Blood_Gp
0 Ravi 38 123 B+
1 Kavi 34 234 A+
2 Chavi 37 345 AB+

You might also like