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

Python Basics 1689905586

This document provides an overview of basic data types and operations in Python. It discusses: 1. Primary data types like numbers, booleans, and strings. Secondary data types like lists, tuples, dictionaries, and sets. 2. Common list operations like accessing/slicing elements, modifying/deleting elements, and iterating through lists. It demonstrates how to generate sequences, take slices of different indices, and use basic list methods. 3. The differences between mutable and immutable objects and how they behave when assigned to other variables or modified. Lists, dictionaries and sets are mutable while numbers, strings and tuples are immutable. 4. Commonly used functions for generating data, accessing/modifying elements

Uploaded by

Romit Maity
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Python Basics 1689905586

This document provides an overview of basic data types and operations in Python. It discusses: 1. Primary data types like numbers, booleans, and strings. Secondary data types like lists, tuples, dictionaries, and sets. 2. Common list operations like accessing/slicing elements, modifying/deleting elements, and iterating through lists. It demonstrates how to generate sequences, take slices of different indices, and use basic list methods. 3. The differences between mutable and immutable objects and how they behave when assigned to other variables or modified. Lists, dictionaries and sets are mutable while numbers, strings and tuples are immutable. 4. Commonly used functions for generating data, accessing/modifying elements

Uploaded by

Romit Maity
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

7/20/2020 Basic@python

Data Types in Python


Basic/Primary Data Types
1. Numbers (Int, Float)
2. Booleans
3. Strings

Containers/Secondary Data Types


1. Lists = [1,2,3]
2. Tuples = (1,2,3)
3. Dictionaries = {1:'Apple',2:'Google',3:'IBM'}
4. Sets = ('Apple','Google','IBM',)

In higher level packages


1. DataFrame from Pandas
2. Array from numpy

1. Lists
In [11]:

## Empty list
my_list = []
print(my_list)

## Pre-initialized list
height = [1,2,3, "Rohit"]
print(height)

[]
[1, 2, 3, 'Rohit']

### Functions to modify/delete data in list

Differences in R and Python

Size of Container

file:///C:/Users/Geethika Reddy/Downloads/Basic_python_lyst9955 (1).html 1/26


7/20/2020 Basic@python

In [12]:

### R uses length()


### Python uses len()

l1 = [10, 20, 30, 40, 50]


print('Length of list1 =',len(l1))

d1 = {1:'Apple',2:'Google',3:'IBM'}
print('Lenth of dictionary =',len(d1))

Length of list1 = 5
Lenth of dictionary = 3

How to know Data Type?


In [14]:

## R uses class()
## Python uses type()

x = 'Rohit'
y = '2017'
z = 2017
print(type(x),type(y),type(z))

<class 'str'> <class 'str'> <class 'int'>

In [18]:

a = [10, 20, 30, 40, 50]


a[4]

Out[18]:

50

R indexing starts from 1

Python indexing starts from 0

In [19]:

a = [1,2,3]
b = a
b[1] = 99
print(a)

[1, 99, 3]

file:///C:/Users/Geethika Reddy/Downloads/Basic_python_lyst9955 (1).html 2/26


7/20/2020 Basic@python

Mutable vs Immutable
Mutable objects allow partial edits. List, Dictionary, Set

Immutable objects can be considered static/constant. You cannot make partial edits. Numeric, Boolean,
String, Tuple

In [20]:

## Mutable objects
my_list = [2,5,6,7]
print(my_list)

my_list[2] = 99
print(my_list)

[2, 5, 6, 7]
[2, 5, 99, 7]

In [23]:

## Immutable objects
my_tuple = (2,5,6,7)
print(my_tuple)

my_tuple[2] = 99
print(my_tuple)

(2, 5, 6, 7)

--------------------------------------------------------------------------
-
TypeError Traceback (most recent call las
t)
<ipython-input-23-05d84a750e0a> in <module>()
3 print(my_tuple)
4
----> 5 my_tuple[2] = 99
6 print(my_tuple)

TypeError: 'tuple' object does not support item assignment

In [24]:

## Behaviour with Immutable objects


x = "Rohit"
print(x,id(x))
x = "Saigal"
print(x,id(x))
## Notice that the memory location gets changed if you modify atomic immutable data typ
es

Rohit 1590240243360
Saigal 1590240888568

file:///C:/Users/Geethika Reddy/Downloads/Basic_python_lyst9955 (1).html 3/26


7/20/2020 Basic@python

In [9]:

## Behavious

In Python variable assignment is by reference


In [26]:

Apple = 20
Orange = Apple
print("Apple",Apple,id(Apple))
print("Orange",Orange,id(Orange))

## Notice that the memory location of both the variables is same

Apple 20 1980788800
Orange 20 1980788800

In [35]:

## Behaviour with Immutable objects


Banana = 20
Mango = Banana
print("Banana",Banana,id(Banana))
print("Mango",Mango,id(Mango),"\n")

Banana = Banana + 5
print("Banana",Banana,id(Banana))
print("Mango",Mango,id(Mango))

Banana 20 1463382080
Mango 20 1463382080

Banana 25 1463382240
Mango 20 1463382080

In [28]:

## Behaviour with mutable objects


Fruits = [20,30,40,50]
Veggies = Fruits
print("Fruits",Fruits,id(Fruits))
print("Veggies",Veggies,id(Veggies),"\n")

Fruits[3] = 99
print("Fruits",Fruits,id(Fruits))
print("Veggies",Veggies,id(Veggies))

Fruits [20, 30, 40, 50] 1590240837448


Veggies [20, 30, 40, 50] 1590240837448

Fruits [20, 30, 40, 99] 1590240837448


Veggies [20, 30, 40, 99] 1590240837448

file:///C:/Users/Geethika Reddy/Downloads/Basic_python_lyst9955 (1).html 4/26


7/20/2020 Basic@python

Essential operations in programming/data engineering


1. Generating Data
2. Accessing/Slicing Data
3. Modifying/Deleting Data
4. Iterating through the Data/Container

Generating a sequence

In [30]:

## Creates sequence of 20 elements starting from 0


sequence1 = list(range(20))
print('sequence1 =',sequence1)

## Custom sequence from specific start and end points


sequence2 = list(range(50,60))
print('sequence2 =',sequence2)

## Custom sequence from specific start and end points with variable leap
sequence3 = list(range(100,150,10))
print('sequence3 =',sequence3)

sequence1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19]
sequence2 = [50, 51, 52, 53, 54, 55, 56, 57, 58, 59]
sequence3 = [100, 110, 120, 130, 140]

Note - start argument in inclusive but end argument in exclusive

In [42]:

nums = list(range(5)) # range is a built-in function that creates a list of integer


s
print(nums) # Prints "[0, 1, 2, 3, 4]"
print(nums[2:4]) # Get a slice from index 2 to 4 (exclusive); prints "[2, 3]"
print(nums[2:]) # Get a slice from index 2 to the end; prints "[2, 3, 4]"
print(nums[:4]) # Get a slice from the start to index 2 (exclusive); prints "
[0, 1]"
print(nums[:]) # Get a slice of the whole list; prints "[0, 1, 2, 3, 4]"prin
t(nums[:-1])

[0, 1, 2, 3, 4]
[2, 3]
[2, 3, 4]
[0, 1, 2, 3]
[0, 1, 2, 3, 4]

file:///C:/Users/Geethika Reddy/Downloads/Basic_python_lyst9955 (1).html 5/26


7/20/2020 Basic@python

In [31]:

## Negative Slicing
nums = list(range(5))
print(nums[-1:]) # Last 2 elements "[3,4]"
print(nums[:-1]) # All elements from starting till second to last (Excluding l
ast element) "[0, 1, 2, 3]"

[4]
[0, 1, 2, 3]

In [32]:

## Slicing with leaps


nums = list(range(10))
print(nums)

print(nums[2:9:3]) # Printing every 3rd element starting from 1st(inclusive) ind


ex till 8th(exclusive)
print(nums[:8:3]) # Printing every 3rd element from the beginning till 8th(excl
usive)
print(nums[5::2]) # Printing every 2nd element from 5th(inclusive) index till t
he end

## Negative leap will results in reverse iterations


print(nums[::-1]) # Printing list in reverse order
print(nums[5::-1]) # Reverse order - i.e. 5th(inclusive) index till beginning
print(nums[7:2:-2]) # Printing every 2nd element in reverse order from 7th(inclus
ive) till 2nd(exclusive)

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[2, 5, 8]
[0, 3, 6]
[5, 7, 9]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
[5, 4, 3, 2, 1, 0]
[7, 5, 3]

Functions to modify/delete data

file:///C:/Users/Geethika Reddy/Downloads/Basic_python_lyst9955 (1).html 6/26


7/20/2020 Basic@python

In [17]:

## List functions
l1 = list(range(10))
l1[1] = 99 # Updating a value at particular index
print(l1)

l1[5:7] = [55,66,77] # Multiple assignments are fine as long as length of LHS =


RHS
print(l1)

l1.append(777) # Appending only 1 item at the end of list


print(l1)

l1.extend([111,222,333]) # Adding a list to list


print(l1)

print(l1.index(777)) # Find the index of first occurence

l1.remove(777) # Delete an object from list


print(l1)

l1.reverse() # Reverse the list

## For more functions please refer python documentation or online sources

[0, 99, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 99, 2, 3, 4, 55, 66, 77, 7, 8, 9]
[0, 99, 2, 3, 4, 55, 66, 77, 7, 8, 9, 777]
[0, 99, 2, 3, 4, 55, 66, 77, 7, 8, 9, 777, 111, 222, 333]
11
[0, 99, 2, 3, 4, 55, 66, 77, 7, 8, 9, 111, 222, 333]

Iterating in a Container

In [33]:

# Using for loop


animals = ['cat', 'dog', 'monkey']
for i in animals:
print(i)

cat
dog
monkey

In [34]:

# Iterating with index


animals = ['cat', 'dog', 'monkey']
for idx, animal in enumerate(animals):
print('Index %d: %s' % (idx + 1, animal))

Index 1: cat
Index 2: dog
Index 3: monkey

file:///C:/Users/Geethika Reddy/Downloads/Basic_python_lyst9955 (1).html 7/26


7/20/2020 Basic@python

Why List Comprehension?

In [35]:

## List of squares using for Loop


nums = list(range(6))
squares = []
for x in nums:
squares.append(x ** 2)
print(squares)

[0, 1, 4, 9, 16, 25]

In [37]:

## List of squares using list comprehension


nums = list(range(6))
squares = [x ** 2 for x in nums]
print(squares)

[0, 1, 4, 9, 16, 25]

In [38]:

## Versatile enough to accomodate conditions


nums = list(range(6))
even_squares = [x ** 2 for x in nums if x % 2 == 0]
print(even_squares)

[0, 4, 16]

Dictionaries
Keys and Values

In [41]:

d = {'cat': 'cute', 'dog': 'furry'} # Create a new dictionary with some data

print(d['cat']) # Get an entry from a dictionary; prints "cute"

print('cat' in d) # Check if a dictionary has a given key; prints "True"

d['fish'] = 'wet' # Add entry in a dictionary

print(d)

cute
True
{'cat': 'cute', 'dog': 'furry', 'fish': 'wet'}

file:///C:/Users/Geethika Reddy/Downloads/Basic_python_lyst9955 (1).html 8/26


7/20/2020 Basic@python

In [64]:

d = {'person': 2, 'cat': 4, 'spider': 8}

for i in d: # By default iterates through keys


print(i)

for i in d.values(): # dictionary.values() will iterate through values


print(i)

person
cat
spider
2
4
8

In [42]:

d = {'person': 2, 'cat': 4, 'spider': 8}

for key, value in d.items(): # dictionary.items() will give you key valu
e pairs
print('A %s has %d legs' % (key, value))

for animal, legs in d.items(): # can be substituted with meaningful names


print('A %s has %d legs' % (animal, legs))

A person has 2 legs


A cat has 4 legs
A spider has 8 legs
A person has 2 legs
A cat has 4 legs
A spider has 8 legs

file:///C:/Users/Geethika Reddy/Downloads/Basic_python_lyst9955 (1).html 9/26


7/20/2020 Basic@python

In [45]:

for i in range(0, 101, 5):


if (i % 2) == 0:
print("%d is even" %(i))
elif (i % 2) != 0:
print("%d is odd" %(i))

0 is even
5 is odd
10 is even
15 is odd
20 is even
25 is odd
30 is even
35 is odd
40 is even
45 is odd
50 is even
55 is odd
60 is even
65 is odd
70 is even
75 is odd
80 is even
85 is odd
90 is even
95 is odd
100 is even

In [46]:

i = 1
while i < 10:
print(i)
i += 1

1
2
3
4
5
6
7
8
9

In [10]:

i = 1
while i < 6:
print(i)
if i == 3:
break
i += 1

1
2
3

file:///C:/Users/Geethika Reddy/Downloads/Basic_python_lyst9955 (1).html 10/26


7/20/2020 Basic@python

In [47]:

i = 0
while i < 6:
i += 1
if i == 3:
continue
print(i)

1
2
4
5
6

In [49]:

i = 1
while i < 6:
print(i)
i += 1
else:
print("i is no longer less than 6")

1
2
3
4
5
i is no longer less than 6

In [51]:

def areaSquare( side ):


area = side * side
return area

In [54]:

side = 4
print(areaSquare(9))

81

In [55]:

class ComplexNumber:
def __init__(self,r = 0,i = 0):
self.real = r
self.imag = i

def getData(self):
print("{0}+{1}j".format(self.real,self.imag))

file:///C:/Users/Geethika Reddy/Downloads/Basic_python_lyst9955 (1).html 11/26


7/20/2020 Basic@python

In [17]:

# Create a new ComplexNumber object


c1 = ComplexNumber(2,3)

# Call getData() function


# Output: 2+3j
c1.getData()

# Create another ComplexNumber object


# and create a new attribute 'attr'
c2 = ComplexNumber(5)
c2.attr = 10

# Output: (5, 0, 10)


print((c2.real, c2.imag, c2.attr))

2+3j
(5, 0, 10)

In [59]:

class Vehicle:
def __init__(self, number_of_wheels, type_of_tank, seating_capacity, maximum_veloci
ty):
self.number_of_wheels = number_of_wheels
self.type_of_tank = type_of_tank
self.seating_capacity = seating_capacity
self.maximum_velocity = maximum_velocity

def number_of_wheels(self):
return self.__number_of_wheels

def number_of_wheels(self, number):


self.__number_of_wheels = number

In [60]:

tesla_model_s = Vehicle(6, 'electric', 5, 250)


maruti_800 = Vehicle(4, 'petrol', 4, 120)

In [61]:

print(tesla_model_s.number_of_wheels)

In [62]:

tesla_model_s.number_of_wheels = 2 # setting number of wheels to 2


print(tesla_model_s.number_of_wheels)

file:///C:/Users/Geethika Reddy/Downloads/Basic_python_lyst9955 (1).html 12/26


7/20/2020 Basic@python

In [ ]:

## https://medium.com/the-renaissance-developer/learning-python-from-zero-to-hero-8ceed
48486d5

In [ ]:

Pandas DataFrame
In [28]:

# import pandas
import pandas as pd

In [30]:

#read matches.csv from data folder


#print ts shape, columns and first 10 index (df.index) and needs to be inside list
# print head of df
df=pd.read_csv('F:/Skillathon/matches.csv')

print(df.shape) # dim() in R - Dimentions of data


print(df.columns) # colnames() in R - Column Namesprint(list(df.index)[:10])
print(df.index) # rownames() in R - Row Indexes
print(df.dtypes) # str() in R - Data types

(577, 18)
Index(['id', 'season', 'city', 'date', 'team1', 'team2', 'toss_winner',
'toss_decision', 'result', 'dl_applied', 'winner', 'win_by_runs',
'win_by_wickets', 'player_of_match', 'venue', 'umpire1', 'umpire2',
'umpire3'],
dtype='object')
RangeIndex(start=0, stop=577, step=1)
id int64
season int64
city object
date object
team1 object
team2 object
toss_winner object
toss_decision object
result object
dl_applied int64
winner object
win_by_runs int64
win_by_wickets int64
player_of_match object
venue object
umpire1 object
umpire2 object
umpire3 float64
dtype: object

file:///C:/Users/Geethika Reddy/Downloads/Basic_python_lyst9955 (1).html 13/26


7/20/2020 Basic@python

In [32]:

df.head(2)

Out[32]:

id season city date team1 team2 toss_winner toss_decision result d

Kolkata Royal Royal


2008-
0 1 2008 Bangalore Knight Challengers Challengers field normal
04-18
Riders Bangalore Bangalore

Chennai
2008- Kings XI Chennai
1 2 2008 Chandigarh Super bat normal
04-19 Punjab Super Kings
Kings

file:///C:/Users/Geethika Reddy/Downloads/Basic_python_lyst9955 (1).html 14/26


7/20/2020 Basic@python

In [34]:

df.tail(10)

Out[34]:

id season city date team1 team2 toss_winner toss_decision

Kolkata
2016- Gujarat Gujarat
567 568 2016 Kanpur Knight field
05-19 Lions Lions
Riders

2016- Sunrisers Delhi Delhi


568 569 2016 Raipur field
05-20 Hyderabad Daredevils Daredevils

2016- Kings XI Rising Pune Kings XI


569 570 2016 Visakhapatnam bat
05-21 Punjab Supergiants Punjab

2016- Mumbai Gujarat Gujarat


570 571 2016 Kanpur field
05-21 Indians Lions Lions

Kolkata
2016- Sunrisers Sunrisers
571 572 2016 Kolkata Knight field
05-22 Hyderabad Hyderabad
Riders

Royal Royal
2016- Delhi
572 573 2016 Raipur Challengers Challengers field
05-22 Daredevils
Bangalore Bangalore

Royal Royal
2016- Gujarat
573 574 2016 Bangalore Challengers Challengers field
05-24 Lions
Bangalore Bangalore

Kolkata Kolkata
2016- Sunrisers
574 575 2016 Delhi Knight Knight field
05-25 Hyderabad
Riders Riders

2016- Gujarat Sunrisers Sunrisers


575 576 2016 Delhi field
05-27 Lions Hyderabad Hyderabad

Royal
2016- Sunrisers Sunrisers
576 577 2016 Bangalore Challengers bat
05-29 Hyderabad Hyderabad
Bangalore

Subsetting DataFrames

file:///C:/Users/Geethika Reddy/Downloads/Basic_python_lyst9955 (1).html 15/26


7/20/2020 Basic@python

In [38]:

# Only Bangalore games


bangalore = df[df['city']=='Bangalore']
#bangalore.head(5)
# Extracting columns
bangalore[['team1','team2','winner']].head()

Out[38]:

team1 team2 winner

0 Kolkata Knight Riders Royal Challengers Bangalore Kolkata Knight Riders

10 Royal Challengers Bangalore Rajasthan Royals Rajasthan Royals

14 Chennai Super Kings Royal Challengers Bangalore Chennai Super Kings

24 Royal Challengers Bangalore Kings XI Punjab Kings XI Punjab

30 Royal Challengers Bangalore Mumbai Indians Mumbai Indians

In [44]:

# Use isin() function to match multiple values in same column


# Get toss_winner from above subset

df[df['season'].isin([2008,2015])][['season','toss_winner']].tail(10)

Out[44]:

season toss_winner

507 2015 Kolkata Knight Riders

508 2015 Sunrisers Hyderabad

509 2015 Kings XI Punjab

510 2015 Rajasthan Royals

511 2015 Royal Challengers Bangalore

512 2015 Sunrisers Hyderabad

513 2015 Mumbai Indians

514 2015 Royal Challengers Bangalore

515 2015 Chennai Super Kings

516 2015 Chennai Super Kings

file:///C:/Users/Geethika Reddy/Downloads/Basic_python_lyst9955 (1).html 16/26


7/20/2020 Basic@python

In [47]:

winner = df['winner'].value_counts() # table() in R - Frequency count


print(winner.head(10))

Mumbai Indians 80
Chennai Super Kings 79
Royal Challengers Bangalore 70
Kolkata Knight Riders 68
Kings XI Punjab 63
Rajasthan Royals 63
Delhi Daredevils 56
Sunrisers Hyderabad 34
Deccan Chargers 29
Pune Warriors 12
Name: winner, dtype: int64

In [50]:

nas = df['winner'].isnull().sum() # is.na() in R - Returns list of boolean


# sum() gives total of na values
print(nas)

Changing index for faster access

In [51]:

# The set_index() function is used to set the DataFrame index using existing columns
df.set_index('date',inplace=True) # Setting date column as index

Indexing
1. loc (Row/Column labels)
2. iloc (Row/Column index)
3. ix (Mixed mode - Depricated)

In [52]:

## loc
print(df.loc[['2008-04-18','2008-04-26'],'city'])

date
2008-04-18 Bangalore
2008-04-26 Bangalore
2008-04-26 Chennai
Name: city, dtype: object

file:///C:/Users/Geethika Reddy/Downloads/Basic_python_lyst9955 (1).html 17/26


7/20/2020 Basic@python

In [53]:

# loc gets rows (or columns) with particular labels from the index
df.loc['2008-04-18':'2008-04-21',['team1','team2']]

Out[53]:

team1 team2

date

2008-04-18 Kolkata Knight Riders Royal Challengers Bangalore

2008-04-19 Chennai Super Kings Kings XI Punjab

2008-04-19 Rajasthan Royals Delhi Daredevils

2008-04-20 Mumbai Indians Royal Challengers Bangalore

2008-04-20 Deccan Chargers Kolkata Knight Riders

2008-04-21 Kings XI Punjab Rajasthan Royals

In [54]:

## Only numeric indexing in iloc


df.iloc[1:3,2:7]

Out[54]:

city team1 team2 toss_winner toss_decision

date

2008-04- Chennai Super Kings XI Chennai Super


Chandigarh bat
19 Kings Punjab Kings

2008-04- Delhi
Delhi Rajasthan Royals Rajasthan Royals bat
19 Daredevils

Missing Values

file:///C:/Users/Geethika Reddy/Downloads/Basic_python_lyst9955 (1).html 18/26


7/20/2020 Basic@python

In [56]:

#filla atribute on df city. inplace true. bfill method


#check with isnull sum
nas_list = df['city'].isnull()
print(df[nas_list].iloc[:,1:5])

season city team1 \


date
2014-04-19 2014 NaN Mumbai Indians
2014-04-19 2014 NaN Kolkata Knight Riders
2014-04-23 2014 NaN Chennai Super Kings
2014-04-25 2014 NaN Sunrisers Hyderabad
2014-04-25 2014 NaN Mumbai Indians
2014-04-28 2014 NaN Royal Challengers Bangalore
2014-04-30 2014 NaN Sunrisers Hyderabad

team2
date
2014-04-19 Royal Challengers Bangalore
2014-04-19 Delhi Daredevils
2014-04-23 Rajasthan Royals
2014-04-25 Delhi Daredevils
2014-04-25 Chennai Super Kings
2014-04-28 Kings XI Punjab
2014-04-30 Mumbai Indians

Imputation
In [57]:

#new_df = df.copy
#new_df['city'].fillna('bfill')('ffill)
df['city'].fillna(method='bfill',inplace=True)
print(df[nas_list].iloc[:,1:5])

season city team1 \


date
2014-04-19 2014 Sharjah Mumbai Indians
2014-04-19 2014 Sharjah Kolkata Knight Riders
2014-04-23 2014 Sharjah Chennai Super Kings
2014-04-25 2014 Abu Dhabi Sunrisers Hyderabad
2014-04-25 2014 Abu Dhabi Mumbai Indians
2014-04-28 2014 Abu Dhabi Royal Challengers Bangalore
2014-04-30 2014 Ranchi Sunrisers Hyderabad

team2
date
2014-04-19 Royal Challengers Bangalore
2014-04-19 Delhi Daredevils
2014-04-23 Rajasthan Royals
2014-04-25 Delhi Daredevils
2014-04-25 Chennai Super Kings
2014-04-28 Kings XI Punjab
2014-04-30 Mumbai Indians

file:///C:/Users/Geethika Reddy/Downloads/Basic_python_lyst9955 (1).html 19/26


7/20/2020 Basic@python

Replace specific values

In [58]:

df[['team1','team2','winner']].replace('Kolkata Knight Riders','KKR').head()

Out[58]:

team1 team2 winner

date

2008-04-18 KKR Royal Challengers Bangalore KKR

2008-04-19 Chennai Super Kings Kings XI Punjab Chennai Super Kings

2008-04-19 Rajasthan Royals Delhi Daredevils Delhi Daredevils

2008-04-20 Mumbai Indians Royal Challengers Bangalore Royal Challengers Bangalore

2008-04-20 Deccan Chargers KKR KKR

Creating DataFrames

In [60]:

dict1 = {'metal':['steel','copper','wood'],'YM':[200,100,50],'Ductile':[True,True,False
]}
df1=pd.DataFrame(dict1,index=['m1','m2','m3'])
df1

Out[60]:

Ductile YM metal

m1 True 200 steel

m2 True 100 copper

m3 False 50 wood

DataFrame Transpose

In [61]:

df1.transpose()

Out[61]:

m1 m2 m3

Ductile True True False

YM 200 100 50

metal steel copper wood

Adding row/column
file:///C:/Users/Geethika Reddy/Downloads/Basic_python_lyst9955 (1).html 20/26
7/20/2020 Basic@python

In [62]:

df1.loc['m4',:] = ['False',0.1,'cement']
df1

Out[62]:

Ductile YM metal

m1 True 200.0 steel

m2 True 100.0 copper

m3 False 50.0 wood

m4 False 0.1 cement

Dropping row/column

In [63]:

print(df1.drop('metal',axis=1))
print(df1.drop('m4',axis=0))

Ductile YM
m1 True 200.0
m2 True 100.0
m3 False 50.0
m4 False 0.1
Ductile YM metal
m1 True 200.0 steel
m2 True 100.0 copper
m3 False 50.0 wood

Appending DataFrame

In [64]:

dict2 = {'metal':['aluminium','bronze'],'YM':[180.2,102.1],'Ductile':[True,False]}
df2 = pd.DataFrame(dict2,index=['m4','m5'])

print(df1.append(df2))

Ductile YM metal
m1 True 200.0 steel
m2 True 100.0 copper
m3 False 50.0 wood
m4 False 0.1 cement
m4 True 180.2 aluminium
m5 False 102.1 bronze

Merging dataframes

file:///C:/Users/Geethika Reddy/Downloads/Basic_python_lyst9955 (1).html 21/26


7/20/2020 Basic@python

In [65]:

df3=pd.DataFrame({'metal':['steel','copper','wood'],'UTS (Mpa)':[400,300,40], 'YM':[20,


10,5]},index=['me1','me2','me3'])
df3

Out[65]:

UTS (Mpa) YM metal

me1 400 20 steel

me2 300 10 copper

me3 40 5 wood

In [67]:

## concat df1 df2


print(pd.concat([df1,df3]))

## merge df1 df3 on metal outer right and left index true
pd.merge(df1,df3,on='metal',how='right')

Ductile UTS (Mpa) YM metal


m1 True NaN 200.0 steel
m2 True NaN 100.0 copper
m3 False NaN 50.0 wood
m4 False NaN 0.1 cement
me1 NaN 400.0 20.0 steel
me2 NaN 300.0 10.0 copper
me3 NaN 40.0 5.0 wood

Out[67]:

Ductile YM_x metal UTS (Mpa) YM_y

0 True 200.0 steel 400 20

1 True 100.0 copper 300 10

2 False 50.0 wood 40 5

Numpy Definition: a Python library used for scientific computing. It can also be
used as an efficient multi-dimensional container for data.

In [2]:

# pip install numpy


import numpy as np

In [3]:

python_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print((python_list) * 2)
print(type(python_list))

[1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9]
<class 'list'>

file:///C:/Users/Geethika Reddy/Downloads/Basic_python_lyst9955 (1).html 22/26


7/20/2020 Basic@python

In [8]:

numpy_array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])


print((numpy_array) * 2)
print(type(numpy_array))
print(numpy_array.dtype)

[ 2 4 6 8 10 12 14 16 18]
<class 'numpy.ndarray'>
int32

In [6]:

print(python_list + python_list)

[1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [9]:

print(numpy_array + numpy_array)

[ 2 4 6 8 10 12 14 16 18]

In [10]:

# Size of an array
numpy_array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
print(numpy_array.size)

In [11]:

# Finding the shape of an array


numpy_matrix = np.array([(1, 2, 3, 4), (5, 6, 7, 8)])
print(numpy_matrix.shape)

(2, 4)

In [31]:

# Reshaping an array
numpy_matrix = np.array([(1, 2, 3, 4), (5, 6, 7, 8)])
numpy_matrix = numpy_matrix.reshape(4, 2)
print(numpy_matrix)

[[1 2]
[3 4]
[5 6]
[7 8]]

In [14]:

# Extracting a specific value from an array


numpy_array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
print(numpy_array[3])

file:///C:/Users/Geethika Reddy/Downloads/Basic_python_lyst9955 (1).html 23/26


7/20/2020 Basic@python

In [16]:

# Extracting multiple values from an array


numpy_array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
print(numpy_array[0:4])

[1 2 3 4]

In [17]:

# Reversing the order of an array


numpy_array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
print(numpy_array[::-1])

[9 8 7 6 5 4 3 2 1]

In [18]:

# Finding the max, min and sum of an array


numpy_array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
print(numpy_array.max())
print(numpy_array.min())
print(numpy_array.sum())

9
1
45

In [43]:

# Finding the mean, median, variance and standard deviation of an array


numpy_array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
print(np.mean(numpy_array))
print(np.median(numpy_array))
print(np.var(numpy_array))
print(np.std(numpy_array))

5.0
5.0
6.666666666666667
2.581988897471611

In [19]:

# Doing an operation on a row and column


print(numpy_matrix)
print("Sum of y-axis",numpy_matrix.sum(axis = 0))
print("Sum of x-axis",numpy_matrix.sum(axis = 1))
print("Overall Sum", numpy_matrix.sum(axis = None))

[[1 2 3 4]
[5 6 7 8]]
Sum of y-axis [ 6 8 10 12]
Sum of x-axis [10 26]
Overall Sum 36

file:///C:/Users/Geethika Reddy/Downloads/Basic_python_lyst9955 (1).html 24/26


7/20/2020 Basic@python

In [20]:

# Joining arrays together to create one array


array_1 = np.array([1,2,3])
array_2 = np.array([4,5,6])
array_3 = np.array([7,8,9])
print(np.concatenate((array_1, array_2, array_3)))

[1 2 3 4 5 6 7 8 9]

In [21]:

array_4 = np.array([[1,2,3],[0,0,0]])
array_5 = np.array([[0,0,0],[7,8,9]])
print(np.concatenate((array_4, array_5), axis = 0))

[[1 2 3]
[0 0 0]
[0 0 0]
[7 8 9]]

In [52]:

# Printing an array filled with zeros and ones


print(np.zeros((1,2)))
print(np.zeros((1,2), dtype = np.int))
print(np.ones((1,2)))
print(np.ones((1,2), dtype = np.int))

[[0. 0.]]
[[0 0]]
[[1. 1.]]
[[1 1]]

In [24]:

# Printing identity matrices


print(np.identity(6))

[[1. 0. 0. 0. 0. 0.]
[0. 1. 0. 0. 0. 0.]
[0. 0. 1. 0. 0. 0.]
[0. 0. 0. 1. 0. 0.]
[0. 0. 0. 0. 1. 0.]
[0. 0. 0. 0. 0. 1.]]

In [55]:

# Creating 8 x 7 matrix with first upper diagonal shifted one over denoted with k = 1
print(np.eye(8, 7, k = 1))

[[0. 1. 0. 0. 0. 0. 0.]
[0. 0. 1. 0. 0. 0. 0.]
[0. 0. 0. 1. 0. 0. 0.]
[0. 0. 0. 0. 1. 0. 0.]
[0. 0. 0. 0. 0. 1. 0.]
[0. 0. 0. 0. 0. 0. 1.]
[0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0.]]

file:///C:/Users/Geethika Reddy/Downloads/Basic_python_lyst9955 (1).html 25/26


7/20/2020 Basic@python

In [26]:

# Multiplying matrices
array_6 = np.array([ 1, 2 ])
array_7 = np.array([ 3, 4 ])
print(np.dot(array_6, array_7))

11

In [ ]:

file:///C:/Users/Geethika Reddy/Downloads/Basic_python_lyst9955 (1).html 26/26

You might also like