PYTHON 3 STANDARD FUNCTIONS LIST COMPREHENSIONS [i for i ...

PYTHON 3

CHEET SHEET BY @CHARLSTOWN

CREATOR: GUIDO VAN ROSSUM YEAR: 1991 LATEST VERSION (2019): 3.7.4 TOP LIBRARIES: TENSORFLOW, SCIKIT-LEARN, NUMPY, KERAS, PYTORCH, PANDAS, SCIPY

ELEMENTAL LIBRARIES import *

import lib > Import all from lib from lib import function > Import function I lib.function() > Import function II dir(math) > Show all functions import library as lb >Library shortcut

STRING OPERATORS "Hello world"

str(29) len("string") "My" + "age is:" + "28" "Hey!" * 3 "a" in "chartlstown" `letters`.isalpha() `string'.upper() `string`.lower() `string`.title() list("string") `my string`.split() "".join(list) "AB".replace("A", "B") string.find("A") " A ".strip() f"My age: {28}" ""My age: {}".format(28) "AB\"CD" "\n" "\t" var = input(`question?')

> Int/Float to string > Total string length > Sum strings > Repeat string by 3 > True if str in str > Check only letters > STR to CAPS-CASE > STR to lower-case > First letter to CAPS > Letters to list > Words to List > List to String by "" > Replace AB > BB > Index from match > No leading spaces > Insert in string > Old insert in string > Include symbol " > New line in string > Tabulator in string >Input string form

INT/FLOAT OPERATORS 3 + 2.56

DICT. OPERATORS

{ky1: "A", ky2: list}

int("25") type() A//B A&B divmod(A, B) len() max() min() abs() pow(5, 2) 5**2 round(A, 3) sum(list)

> String to integer > Returns type > Returns ratio > Returns reminder > Ratio/reminder > Returns lenght > Max. value > Min. value > Absolute value > 5 powered by 2 > 5 powered by 2 > Round 3 decimals > Sum all list items

dic[key] = val dic.update({ky: v, ky: v}) dic[key] = val dic[key] dic.get(key) dic.get(key, DefVal) dic.pop(key) del dic[k] dic.keys() dic.values() dic.items() key in dict dict(zip(lst_1, lst_2))

> Add a key > Add multiple keys > Overwrites value > Extracts a value I > Extracts a value II > Extracts a value III > Delete K and V I > Delete K and V II > Keys List > Values list > Returns K and V > Checks key > Pair lists to Dict.

LOOPS

LIST & DICT COMPREHENSIONS

for item in list: print(item)

> for loop > Iterate by items

LIST COMPREHENSION lst_A = [i for i in lst_B if i < 0]

while limit > Create a DF I

columns = [list]

>> Create a DF II

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

df.head(n)

>> shows first n rows

df.c1.nunique() df.c1.mean() df.c1.median() df.c1.std() df.c1.max() df.c1.min() df.c1.count()

>> Extracts len(set) >> Average of the column >> Median of the column >> Standard deviation >> Max number >> Min number >> len of the set

np.std(lst) >> Standard devi.

np.mean(mtx < n)

>> Conditions

np.var()>> Variance

() >> entries and data EXTRACTING COLUMNS AND ROWS

P.A. (GROOPING) df.groupby(c1).c2.mean()*

>> Groups c1

MATRIX

import numpy as np

mtx = np.array(lst1, lst2, lst3)

np.mean(mtx)

>>Total data mean

np.mean(mtx, axis = 0) >> Columns mean

np.mean(mtx, axis = 1) >> Rows mean

df.column.values df[`colum'] df[[c1, c2]] df.iloc[index] df.iloc[i:i] df[df.c1 > n]

>> Extract column >> Extract multiple clmns >> Extracts columns as df >> Extracts the Row by idx >> Extracts Rows as df >> Extracts Row by cond. I

df.groupby(c1).id.count()* >> Counter

df.groupby(c1).c2.apply(lb)* >> lambda

df.groupby([c1,c2]).c3*

>> Multiple g

* > .reset_index()

>> To reset df

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

df.pivot(columns = c2, index = c1, values = v)

df[df.c1.condition] >> Extracts Row by cond. II MERGE METHODS --------------------

df.reset_index() >> Reset the index

pd.merge(df1, df2*) >> Merge method I

drop = True >> Without inserting it

df1.merge(df2) >> Merge method II

inplace = True >> Modify overwriting

df1.merge(df2).merge(df3)

ADD AND RENAME COLUMNS

* > how = `outer' \ `inner' \ `right' \ `left' --------------------

df[columns] = list >> Adding a column -------------------RENAMING COLUMNS: df.columns = list >> Modifying names df.rename(columns = {old:new}, inplace=True)

pd.merge(df1, df2, left_on = c1, right_on = c3)* >> To merge 2 data frame with same column * > suffixes = [name1, name2] -------------------pd.concat([df1, df2])

APPLY MODIFICATIONS & LAMBDA

SORTING METHODS

df[col] = df.c1.apply() >> Modify column df[col] = df.c1-apply(lb) >> lb = lambda -------------------lb = lambda row: row.c1 >> Lambda in rows df[col] = df.apply(lb, axis = 1)

df.sort_values(by = [`c1`, `c2`], ascending = False)

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download