Python Cheat Sheet

Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

Python Cheat Sheet

Functions:

Unpacking list:

Itertools zip_longest:

[email protected]
KN9QD8MB6E

List comprehensions: Lists that generate themselves within an internal for loop

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited

This file is meant for personal use by [email protected] only.


Sharing or publishing the contents in part or full is liable for legal action.
Loop with logic:

It’s list comprehension:

For list items from 10-19, the numbers will be multiplied by 2 if it is divisible by 2, else the numbers will be multiplied by
3

[email protected]
KN9QD8MB6E

Creating key value pairs:

Enmurate: Adds a counter to an iterable.

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited

This file is meant for personal use by [email protected] only.


Sharing or publishing the contents in part or full is liable for legal action.
Lambda functions
Lambda functions can have any number of arguments but only one expression. The expression is evaluated and
returned. Lambda functions can be used wherever function objects are required.

[email protected]
KN9QD8MB6E

Reduce function: Used when you need to apply a function to an iterable and reduce it to a single cumulative value.

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited

This file is meant for personal use by [email protected] only.


Sharing or publishing the contents in part or full is liable for legal action.
Recursion

A function which calls itself. How this works?

Steps:

1. Returns Number 5*factorial(4)


2. Returns 4*factorial(3)
3. Returns 3* factorial(2)
4. Returns 2* factorial(1)
5. Returns 1
6. Back to Step 4: 2*1=2
7. Back to Step 3: 3*2 =6
8. Back to Step 2: 4*6= 4*3*2=24
9. Back to Step 1: 5*24=120.
[email protected]
KN9QD8MB6E
Here in step1, factorial 4 is replaced by step 2. Read it as 5*4*factorial(3). In step 2, factorial(3 ) is read as 3* factorial(2)
and so on.

So, ideally this is 5*4*3*2*1

Numpy:

numpy is a core library written in c, c++. the top few layers for user interface is in python the rest is all precompiled
libraries. this is how python platform makes up for its speed numpy is the core library for scientific computing in python.
Numpy is general purpose array processing package. it provides a high-performance multidimensional array object and
tools for working with these arrays

Creation of Matrix

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited

This file is meant for personal use by [email protected] only.


Sharing or publishing the contents in part or full is liable for legal action.
[email protected]
KN9QD8MB6E

It is recommended not to change the data type. let numpy decide based on the data.

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited

This file is meant for personal use by [email protected] only.


Sharing or publishing the contents in part or full is liable for legal action.
[email protected]
KN9QD8MB6E

If we have number of elements as 6 shape elements have to be factor of that. it can’t be 2x1 or 2x4

What happens here is we know total number of elements is 6 and we pass on dimension to be 3 rows, the other dimension
We dont want to calculate, numpy will do that for us and give 3x2. we have to pass the other dimension as a negative value

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited

This file is meant for personal use by [email protected] only.


Sharing or publishing the contents in part or full is liable for legal action.
What happens here is we know total number of elements is 6 and we pass one dimension to be 3 rows, the other dimension
we dont want to calculate, numpy will do that for us and give 3x2. we have to pass the other dimension as a negative value

These are pseudo random numbers. If we use seed, we'll always get same values.

[email protected]
KN9QD8MB6E

2D split

hsplit: Split an array into multiple sub-arrays horizontally (column-wise). Split with axis=1.

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited

This file is meant for personal use by [email protected] only.


Sharing or publishing the contents in part or full is liable for legal action.
[email protected]
KN9QD8MB6E

Head split splits columns and groups columns together as a numpy array in a list. So, since we did headsplit and 4 as
parameter, we get 4 splits. since we have 6x8, 6 rows and 8 columns and we need 4 splits, 8 columns is split 4 times
(8/4)=2 so, 2 columns grouped together as one array-> with 6 rows and 2 columns, and we such arrays in a list

Please note: the split must be a factor of the dimension or it throws an error.

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited

This file is meant for personal use by [email protected] only.


Sharing or publishing the contents in part or full is liable for legal action.
[email protected]
KN9QD8MB6E

Example:

col- 012, 34, 5, 67, get these mentioned columns grouped


together

so, if we use a vector those indices will be used for slicing


purposes

We have successfully split columns 012, 34, 5, 67 as individual groups as we can see above.

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited

This file is meant for personal use by [email protected] only.


Sharing or publishing the contents in part or full is liable for legal action.
[email protected]
KN9QD8MB6E

2D Stack
In 3D if we are doing head stacking, other 2 dimensions will remain same.
row and column remain same, depth changes, for example, a book with 10 pages and another book with 20 pages is taken.
When we stack those two together, row and column remains same, depth becomes 10+20=30. that’s hstack

in 2D stacking one-dimension changes, other remains same


in n dimension, nth dim changes, remaining (n-1) remains same

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited

This file is meant for personal use by [email protected] only.


Sharing or publishing the contents in part or full is liable for legal action.
[email protected]
KN9QD8MB6E

Broadcast
In pandas we use the word concatenation, here we use the word stack

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited

This file is meant for personal use by [email protected] only.


Sharing or publishing the contents in part or full is liable for legal action.
[email protected]
KN9QD8MB6E

Iteration

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited

This file is meant for personal use by [email protected] only.


Sharing or publishing the contents in part or full is liable for legal action.
3D creation

Whenever you see a "Set of numbers" closed in double brackets from both ends. Consider it as a "set". We have
two sets, each set with 3 rows and 4 columns.
[email protected]
KN9QD8MB6E
Pandas
To read dataset:

To view the first five rows:

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited

This file is meant for personal use by [email protected] only.


Sharing or publishing the contents in part or full is liable for legal action.
To view last 3 rows:

To read column headers:

To access only one row of a dataframe:

[email protected]
KN9QD8MB6E

If we want to access more than one column of a dataframe we can use list for columns and indexing to get rows:

Indexing to access rows:

To get first row:

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited

This file is meant for personal use by [email protected] only.


Sharing or publishing the contents in part or full is liable for legal action.
To get rows 1, 2 and 3

To read a value at a specific location: df.iloc[row,col]

[email protected]
KN9QD8MB6E

You can also use iterrows to access each row and get its index:

Using condition to get rows from data frame accordingly:

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited

This file is meant for personal use by [email protected] only.


Sharing or publishing the contents in part or full is liable for legal action.
Statistical description of the dataframe columns:

Sort values based on column:


[email protected]
KN9QD8MB6E

Sort values based on two columns and arrange each column in ascending or descending order.

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited

This file is meant for personal use by [email protected] only.


Sharing or publishing the contents in part or full is liable for legal action.
Filtering data:

Filtering the columns based particular conditions.

[email protected]
KN9QD8MB6E

To retain old index:

To not retain old index, drop=True.

Use the ~ operator to remove rows based on a certain condition.

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited

This file is meant for personal use by [email protected] only.


Sharing or publishing the contents in part or full is liable for legal action.
To make changes based on condition:

To change columns based on conditions, the columns that needs to be changed is the columns that we specify as a list:

To group columns based on a type and get the values in the other columns based on rows that contain this information:

[email protected]
KN9QD8MB6E

To concat two dataframes:

Here, we use dictionaries to create dataframes, with keys being the column headers and the values in the list being the
column values.

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited

This file is meant for personal use by [email protected] only.


Sharing or publishing the contents in part or full is liable for legal action.
We say, axis=0 to concatenate them across rows and axis=1, to concatenate them across columns. By default, it has
axis=0

Apply function to apply the logic of the function to the columns in dataframe:

[email protected]
KN9QD8MB6E
To apply the function to each element on the dataframe.

Pivot table:

Based on the index column, it gives you the summary as mean of the other columns for each category in the index
column.

For example:

Here, index is brand and based on each brand we get the mean mileage, price and year here.

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited

This file is meant for personal use by [email protected] only.


Sharing or publishing the contents in part or full is liable for legal action.
We can also create our own aggfunc and then call it as the parameter value in the pivot_table. Here, the values have the
column that we need to apply the sum or average to based on the index column. So, for each value in the index column,
we calculate the sum or average of the value column or both based on the aggfunc.

Source for Pandas: https://www.youtube.com/watch?v=vmEHCJofslg

[email protected]
KN9QD8MB6E

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited

This file is meant for personal use by [email protected] only.


Sharing or publishing the contents in part or full is liable for legal action.

You might also like