Python Cheat Sheet
Python Cheat Sheet
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
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
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
[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
Steps:
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
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
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
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
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
Example:
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
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
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
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
Iteration
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
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:
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
[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:
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
[email protected]
KN9QD8MB6E
You can also use iterrows to access each row and get its index:
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
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
[email protected]
KN9QD8MB6E
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
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
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
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
[email protected]
KN9QD8MB6E
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited