0% found this document useful (0 votes)
6 views7 pages

L-1 (Introduction To Numpy & Panda) - Colab

The document provides an introduction to Numpy and Pandas, covering the creation and manipulation of arrays and data frames. It includes examples of creating 1D and 2D arrays, indexing, slicing, and generating random values using Numpy, as well as creating and managing Series and DataFrames in Pandas. The document also demonstrates how to read data from a CSV file and modify DataFrames by adding new columns and changing row labels.

Uploaded by

ashishpal2804
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)
6 views7 pages

L-1 (Introduction To Numpy & Panda) - Colab

The document provides an introduction to Numpy and Pandas, covering the creation and manipulation of arrays and data frames. It includes examples of creating 1D and 2D arrays, indexing, slicing, and generating random values using Numpy, as well as creating and managing Series and DataFrames in Pandas. The document also demonstrates how to read data from a CSV file and modify DataFrames by adding new columns and changing row labels.

Uploaded by

ashishpal2804
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
You are on page 1/ 7

3/14/25, 4:34 PM L-1 (Introduction to Numpy & Panda) - Colab

keyboard_arrow_down Numpy Arrays


Create an array from list using numpy

import numpy as np
data1=[2,3,4,5]
arr1= np.array(data1)
print(arr1)

[2 3 4 5]

Create 2d array from list using numpy

data1=[[2,3,4,5],[34,45,56,30]]
arr2=np.array(data1)
print(arr2)

[[ 2 3 4 5]
[34 45 56 30]]

Print dimnesions, shape and type of an array

print(arr2.ndim)
print(arr2.shape)
print(arr2.dtype)

2
(2, 4)
int64

Create 1D and 2D array of Zeros

arr1=np.zeros(10)
arr2=np.zeros((3,6))
print("1d array of Zeros:",arr1)
print("2d array of Zeros")
print(arr2)

1d array of Zeros: [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]


1d array of Zeros
[[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]]

Create 1D and 2D array of sequence

arr1=np.arange(15)
arr2=np.arange(15).reshape(3,5)
print("1d array of sequence:",arr1)
print("2d array of sequence")
print(arr2)

1d array of sequence: [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]


2d array of sequence
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]]

Create 1D and 2D array of Random Values

a1 = np.random.rand(7)
print("1D:",a1)
a2 = np.random.rand(2,4)

https://colab.research.google.com/drive/1L_nq3557ODX9-DPKl7K2qHZwuY-BUC-8#printMode=true 1/7
3/14/25, 4:34 PM L-1 (Introduction to Numpy & Panda) - Colab
print("2D:",a2)
print()
print("1D and 2D Array of Random Integer Values")
a3 = np.random.randint(7,size=(5))
print("1D:",a3)
a4 = np.random.randint(7, size=(2,4))
print("2D:",a4)

1d: [0.50381422 0.97820965 0.76875114 0.41566765 0.08396744 0.81772727


0.92097797]
2d: [[0.66793227 0.88952303 0.02239508 0.11735786]
[0.72608375 0.04330168 0.57570961 0.90343898]]

1d and 2d Array of Random Integer Values


1d: [1 1 1 2 2]
2d: [[6 5 3 1]
[5 1 5 0]]

1D Array Indexing and Slicing

print(arr1[5:8])
print(arr1[:])
print(arr1[1:])
print(arr1[4:])
print(arr1[:3])

[5 6 7]
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]
[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14]
[ 4 5 6 7 8 9 10 11 12 13 14]
[0 1 2]

2D Array Indexing and Slicing

print("original array")
print(arr2)
print("0th row : all columns")
print(arr2[0,:])
print("all rows : 1st columns")
print(arr2[:,1])
print("1 to last rows : 2 to last columns")
print(arr2[1:,2:])

original array
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]]
0th row : all columns
[0 1 2 3 4]
all rows : 1st columns
[ 1 6 11]
1 to last rows : 2 to last columns
[[ 7 8 9]
[12 13 14]]

keyboard_arrow_down Pandas : Series & Data Frames


Create Series

import pandas as pd

s1=pd.Series([2,3,4,5])
print(s1)

0 2
1 3
2 4
https://colab.research.google.com/drive/1L_nq3557ODX9-DPKl7K2qHZwuY-BUC-8#printMode=true 2/7
3/14/25, 4:34 PM L-1 (Introduction to Numpy & Panda) - Colab
3 5
dtype: int64

s1=pd.Series([2,3,4,5], index=['a','b','c','d'])
s2 = pd.Series([1000,2000,-1000,-5000,1000],index = ['a', 'b', 'c', 'd', 'e'])
s3 = pd.Series([10,20,-10,-50,100], index = ['z', 'y', 'a', 'c', 'e'])
print(s1)
print(s2)
print(s3)

a 2
b 3
c 4
d 5
dtype: int64
a 1000
b 2000
c -1000
d -5000
e 1000
dtype: int64
z 10
y 20
a -10
c -50
e 100
dtype: int64

arr4=pd.Series(np.arange(5),index=['a','b','c','d','e'])
print(arr4)

a 0
b 1
c 2
d 3
e 4
dtype: int64

Create Series using Dictionary

dic1={'one':23,'two':34,'three':45}
s2=pd.Series(dic1)
print(s2)

one 23
two 34
three 45
dtype: int64

Drop Rows from Series

arr5=arr4.drop('d')
print(arr5)

a 0
b 1
c 2
e 4
dtype: int64

arr5=arr4.drop(['a','d'])
print(arr5)

b 1
c 2
e 4
dtype: int64

Data Frame

https://colab.research.google.com/drive/1L_nq3557ODX9-DPKl7K2qHZwuY-BUC-8#printMode=true 3/7
3/14/25, 4:34 PM L-1 (Introduction to Numpy & Panda) - Colab

Create Data Frame Using List

lst = ['Delhi', 'is', 'the', 'capital', 'of', 'India']


df = pd.DataFrame(lst)
df

0 Delhi

1 is

2 the

3 capital

4 of

5 India

Calling DataFrame constructor on list with row labels

df = pd.DataFrame(lst, columns=["WORDS"], index = ['a', 'b', 'c', 'd', 'e', 'f'])


print(df)
type(df)

WORDS
a Delhi
b is
c the
d capital
e of
f India
pandas.core.frame.DataFrame

Create Data Frame Using Arrange

frame1= pd.DataFrame(np.arange(9).reshape((3,3)), index=['a','b','c'],columns=['n1','n2','n3'])


print(frame1)

n1 n2 n3
a 0 1 2
b 3 4 5
c 6 7 8

Create Data Frame Using Series without indexing

s1=pd.Series([2,3,4,5,6])
s2 = pd.Series([1000,2000,-1000,-5000,1000])
s3 = pd.Series([10,20,-10,-50,100])
df = pd.DataFrame([s1, s2, s3] )
print(df)

0 1 2 3 4
0 2 3 4 5 6
1 1000 2000 -1000 -5000 1000
2 10 20 -10 -50 100

Create Data Frame Using Series with indexing

s1=pd.Series([2,3,4,5,6], index=['a','b','c','d','e'])
s2 = pd.Series([1000,2000,-1000,-5000,1000],index = ['a', 'b', 'c', 'd', 'e'])
s3 = pd.Series([10,20,-10,-50,100], index = ['z', 'y', 'a', 'c', 'e'])
df = pd.DataFrame([s1, s2, s3] )
print(df)

a b c d e z y
0 2.0 3.0 4.0 5.0 6.0 NaN NaN
1 1000.0 2000.0 -1000.0 -5000.0 1000.0 NaN NaN
2 -10.0 NaN -50.0 NaN 100.0 10.0 20.0

https://colab.research.google.com/drive/1L_nq3557ODX9-DPKl7K2qHZwuY-BUC-8#printMode=true 4/7
3/14/25, 4:34 PM L-1 (Introduction to Numpy & Panda) - Colab

Create Data Frame Using Dictionary

ddict={'a':20, 'b':25, 'c':35, 'd':60}


df=pd.DataFrame(list(ddict.items()))
df

0 1

0 a 20

1 b 25

2 c 35

3 d 60
 

dict1 = {'State': ['Assam', 'Delhi', 'Kerala'],


'GArea': [78438, 1483, 38852] ,
'VDF' : [2797, 6.72,1663]}
df= pd.DataFrame(dict1)
print(df)

State GArea VDF


0 Assam 78438 2797.00
1 Delhi 1483 6.72
2 Kerala 38852 1663.00

Create dataframe using Dictionary of Dictionaries

df_dod = {'Nevada': {2001: 2.4, 2002: 2.9},


'Ohio': {2000: 1.5, 2001: 1.7, 2002: 3.6}}
df = pd.DataFrame(df_dod)
print(df)

Nevada Ohio
2001 2.4 1.7
2002 2.9 3.6
2000 NaN 1.5

Create dataframe using file on google drive

from google.colab import drive


drive.mount('/content/drive', force_remount=True) # Mount the Drive
df = pd.read_csv("/content/drive/MyDrive/Data_Analytics/Q6.csv") # Read the file and convert into Dataframe
df

Mounted at /content/drive
Name Gender MonthlyIncome

0 Shah Male 114000

1 Vats Male 65000

2 Vats Female 43150

3 Kumar Female 69500

4 Vats Female 155000

5 Kumar Male 103000

6 Shah Male 55000

7 Shah Female 112400

8 Kumar Female 81030

9 Vats Male 71900


 

Transpose of Data Frame

https://colab.research.google.com/drive/1L_nq3557ODX9-DPKl7K2qHZwuY-BUC-8#printMode=true 5/7
3/14/25, 4:34 PM L-1 (Introduction to Numpy & Panda) - Colab
df.T

0 1 2 3 4 5 6 7 8 9

Name Shah Vats Vats Kumar Vats Kumar Shah Shah Kumar Vats

Gender Male Male Female Female Female Male Male Female Female Male

MonthlyIncome 114000 65000 43150 69500 155000 103000 55000 112400 81030 71900
 

ADD a new Column to Data Frame

df['age']=[40,30,40,40,30,45,50,25,30,30]
df

Name Gender MonthlyIncome age

0 Shah Male 114000 40

1 Vats Male 65000 30

2 Vats Female 43150 40

3 Kumar Female 69500 40

4 Vats Female 155000 30

5 Kumar Male 103000 45

6 Shah Male 55000 50

7 Shah Female 112400 25

8 Kumar Female 81030 30

9 Vats Male 71900 30


 

*Change the Row Lables of Data Frame *

df.index = ['f1','f2','f3','f4','f5','f6','f7','f8','f9','f10']
df

Name Gender MonthlyIncome age

f1 Shah Male 114000 40

f2 Vats Male 65000 30

f3 Vats Female 43150 40

f4 Kumar Female 69500 40

f5 Vats Female 155000 30

f6 Kumar Male 103000 45

f7 Shah Male 55000 50

f8 Shah Female 112400 25

f9 Kumar Female 81030 30

f10 Vats Male 71900 30


 

Change Column Labels of Data Frame

df.columns=["NAME","GENDER","MONTHLY_INCOME","AGE"]
df

https://colab.research.google.com/drive/1L_nq3557ODX9-DPKl7K2qHZwuY-BUC-8#printMode=true 6/7
3/14/25, 4:34 PM L-1 (Introduction to Numpy & Panda) - Colab

NAME GENDER MONTHLY_INCOME AGE

f1 Shah Male 114000 40

f2 Vats Male 65000 30

f3 Vats Female 43150 40

f4 Kumar Female 69500 40

f5 Vats Female 155000 30

f6 Kumar Male 103000 45

f7 Shah Male 55000 50

f8 Shah Female 112400 25

f9 Kumar Female 81030 30

f10 Vats Male 71900 30


 

https://colab.research.google.com/drive/1L_nq3557ODX9-DPKl7K2qHZwuY-BUC-8#printMode=true 7/7

You might also like