451 - 503 Flashcards

1
Q

matplotlib.contour()

A

used to plot contours.

x, y = np.mgrid[-3*np.pi:3*np.pi:100j, -3*np.pi:3*np.pi:100j]
z = np.sinc(x) + np.cos(y)

fig, ax = plt.subplots()
ax.contour(z)

fig.set_figwidth(8)     #  ширина и
fig.set_figheight(8)    #  высота "Figure"
x, y = np.mgrid[-3*np.pi:3*np.pi:300j, -3*np.pi:3*np.pi:300j]
z = np.sinc(x) + np.cos(y)

fig, ax = plt.subplots()
ax.contour(z, levels = 20)

fig.set_figwidth(12)     #  ширина и
fig.set_figheight(12)    #  высота "Figure"

plt.show()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

CI and CD

A

«непрерывное развертывание», — это методика разработки программного обеспечения, реализуемая благодаря инструментам автоматизации. Регулярные и надежные обновления уменьшают циклы выпуска за счет непрерывной доставки кода.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

numpy.genfromtxt(fname, dtype=<class ‘float’>, comments=’#’, delimiter=None, skip_header=0, skip_footer=0, converters=None, missing_values=None, filling_values=None, usecols=None, names=None, excludelist=None, deletechars=” !#$%&’()*+, -./:;<=>?@[\]^{|}~”, replace_space=’_’, autostrip=False, case_sensitive=True, defaultfmt=’f%i’, unpack=None, usemask=False, loose=True, invalid_raise=True, max_rows=None, encoding=’bytes’, *, ndmin=0, like=None)

A

Load data from a text file, with missing values handled as specified.

Each line past the first skip_header lines is split at the delimiter character, and characters
following the comments character are discarded.

data = np.genfromtxt(s, dtype=[('myint','i8'),('myfloat','f8'), ('mystring','S5')], delimiter=",")
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

pandas.read_stata(filepath_or_buffer, *, convert_dates=True, convert_categoricals=True, index_col=None, convert_missing=False, preserve_dtypes=True, columns=None, order_categoricals=True, chunksize=None, iterator=False, compression=’infer’, storage_options=None)

A

Read Stata file into DataFrame.

df = pd.read_stata('animals.dta')
itr = pd.read_stata('filename.dta', chunksize=10000)  
for chunk in itr:
   # Operate on a single chunk, e.g., chunk.mean()
	    pass
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

pandas.Series.to_frame(name=_NoDefault.no_default)

A

is used to convert the given series object to a dataframe.

s = pd.Series(["a", "b", "c"], name="vals")

s.to_frame()
        vals
0       a
1       b
2       c
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

pandas.ExcelFile.parse(sheet_name=0, header=0, names=None, index_col=None, usecols=None, squeeze=None, converters=None, true_values=None, false_values=None, skiprows=None, nrows=None, na_values=None, parse_dates=False, date_parser=None, thousands=None, comment=None, skipfooter=0, convert_float=None, mangle_dupe_cols=True, **kwds)

A

Parse specified sheet(s) into a DataFrame.

Equivalent to read_excel(ExcelFile, …) See the read_excel docstring for more info on accepted parameters.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

pandas.read_excel(io, sheet_name=0, *, header=0, names=None, index_col=None, usecols=None, squeeze=None, dtype=None, engine=None, converters=None, true_values=None, false_values=None, skiprows=None, nrows=None, na_values=None, keep_default_na=True, na_filter=True, verbose=False, parse_dates=False, date_parser=None, thousands=None, decimal=’.’, comment=None, skipfooter=0, convert_float=None, mangle_dupe_cols=True, storage_options=None)

A

Read an Excel file into a pandas DataFrame. Supports xls, xlsx, xlsm, xlsb, odf, ods and odt file extensions read from a local filesystem or URL. Supports an option to read a single sheet or a list of sheets.

pd.read_excel('tmp.xlsx', index_col=0)
pd.read_excel(open('tmp.xlsx', 'rb'), sheet_name='Sheet3')
pd.read_excel('tmp.xlsx', index_col=None, header=None)
pd.read_excel('tmp.xlsx', index_col=0, comment='#')
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

string.isalpha()

A

method returns True if all the characters are alphabet letters (a-z).

txt = "Company10"
x = txt.isalpha()

print(x)
👉 False
txt = "CompanyX"
x = txt.isalpha()

print(x)
👉 True
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

LinkedIn BOOLEAN SEARCH (AND, OR, NOT, (), “”)

A

allow you to specify the search.

  • OR → или то или иное, любой из вариантов
  • AND → для поиска результатов включающих все параметры (including all)
  • ”” → должны присутствовать слова и в том же порядке как указано внутри скобок
  • NOT → убрать из поиска определенные слова или фразы
  • () → то фильтр, что внутри должен использоваться первым
"Software Developer" OR "Python Developer" OR "Data Analyst" OR "Data Engineer" OR "Data Scientist" OR "Machine Learning"
bookkkeeper OR accountant
ceo OR founder OR entrepreneur
accounting AND law
ceo AND nutrition AND fitness
"freelance writer
"business development manager"
vp OR director NOT assistant
"personal trainer" NOT "weight loss"
"business owner" AND (coach OR consultant) AND (health OR fitness OR nutrition)
"personal trainer" AND (moms OR pregnancy OR "weight loss") NOT injury
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

pyautogui.Window Functions

A
  • pyautogui.getWindows() → returns a dict of window titles mapped to window IDs
  • pyautogui.getWindow(str_title_or_int_id) → returns a “Win” object
  • pyautogui.win.move(x, y)
  • pyautogui.win.resize(width, height)
  • pyautogui.win.maximize()
  • pyautogui.win.minimize()
  • pyautogui.win.restore()
  • pyautogui.win.close()
  • pyautogui.win.position() → returns (x, y) of top-left corner
  • pyautogui.win.moveRel(x=0, y=0) → moves relative to the x, y of top-left corner of the window
  • pyautogui.win.clickRel(x=0, y=0, clicks=1, interval=0.0, button=’left’) → click relative to
    the x, y of top-left corner of the window
  • pyautogui.win.isMinimized()
  • pyautogui.win.isMaximized()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

pyautogui.hold()

A

Press the key down and hold it.

pyautogui.hold('shift')
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

pandas.DataFrame.cumsum(axis=None, skipna=True, *args, **kwargs)

A

used to find the cumulative sum value over any axis. Each cell is populated with the cumulative sum of the values seen so far.

s = pd.Series([2, np.nan, 5, -1, 0])

s.cumsum()
0    2.0
1    NaN
2    7.0
3    6.0
4    6.0
s = pd.Series([2, np.nan, 5, -1, 0])
s.cumsum(skipna=False)
0    2.0
1    NaN
2    NaN
3    NaN
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

pandas.DataFrame.rank(axis=0, method=’average’, numeric_only=None, na_option=’keep’,
ascending=True, pct=False)

A

method returns a rank of every respective index of a series passed. The rank is returned on the basis of position after sorting.

s = pd.Series(range(5), index=list("abcde"))
s["d"] = s["b"]

s.rank()
a    1.0
b    2.5
c    4.0
d    2.5
e    5.0
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

pandas.DataFrame.idxmin(axis=0, skipna=True, numeric_only=False)

A

function to find the index of the minimum value along the index axis.

df = pd.DataFrame({"A":[4, 5, 2, 6],
                   "B":[11, 2, 5, 8],
                   "C":[1, 8, 66, 4]})

df.idxmin(axis = 0)
A    2
B    1
С    0
df = pd.DataFrame({"A":[4, 5, 2, None], 
                   "B":[11, 2, None, 8], 
                   "C":[1, 8, 66, 4]})
  
# Skipna = True will skip all the Na values
df.idxmin(axis = 1, skipna = True)
0    C
1    B
2    A
3    C
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

pandas.read_sql_table(table_name, con, schema=None, index_col=None, coerce_float=True,
parse_dates=None, columns=None, chunksize=None)

A

Read SQL database table into a DataFrame.

pd.read_sql_table('table_name', 'postgres:///db_name')  
cnx = create_engine('sqlite:///students.db').connect()
df = pd.read_sql_table('students', cnx)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

pandas.DataFrame.assign(**kwargs)

A

Assign new columns to a DataFrame. Returns a new object with all original columns in addition to new ones. Existing columns that are re-assigned will be overwritten.

df = pd.DataFrame({'temp_c': [17.0, 25.0]}, index=['Portland', 'Berkeley'])

df.assign(temp_f=lambda x: x.temp_c * 9 / 5 + 32)

                 temp_c         temp_f
Portland    17.0             62.6
Berkeley    25.0             77.0
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

pandas.DataFrame.quantile(q=0.5, axis=0, numeric_only=_NoDefault.no_default,interpolation=’linear’, method=’single’)

A

return values at the given quantile over requested axis. Divides the members of a batch or sample into equal-sized subgroups of adjacent values or a probability distribution into distributions of equal probability.

df = pd.DataFrame({"A":[1, 5, 3, 4, 2],
                   "B":[3, 2, 4, 3, 4],
                   "C":[2, 2, 7, 3, 4],
                   "D":[4, 3, 6, 12, 7]})

df.quantile(.2, axis = 0)
A    1.8
B    2.8
С    2.0
D    3.8
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

pandas.to_datetime(arg, errors=’raise’, dayfirst=False, yearfirst=False, utc=None, format=None, exact=True, unit=None, infer_datetime_format=False, origin=’unix’, cache=True)

A

Convert argument to datetime. This function converts a scalar, array-like, Series or DataFrame/dict-like to a pandas datetime object.

df = pd.DataFrame({'year': [2015, 2016],
                   'month': [2, 3],
                   'day': [4, 5]})
pd.to_datetime(df)
0   2015-02-04
1   2016-03-05
dtype: datetime64[ns]
s = pd.Series(['3/11/2000', '3/12/2000', '3/13/2000'] * 1000)
s.head()
0    3/11/2000
1    3/12/2000
2    3/13/2000
3    3/11/2000
4    3/12/2000
dtype: object
pd.to_datetime(1490195805, unit='s')
Timestamp('2017-03-22 15:16:45')
pd.to_datetime(1490195805433502912, unit='ns')
Timestamp('2017-03-22 15:16:45.433502912')
pd.to_datetime(['2018-10-26 12:00 -0500', '2018-10-26 13:00 -0500'])
DatetimeIndex(['2018-10-26 12:00:00-05:00', '2018-10-26 13:00:00-05:00'],
              dtype='datetime64[ns, pytz.FixedOffset(-300)]', freq=None)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

pandas.DataFrame.nlargest(n, columns, keep=’first’)

A

Return the first n rows with the largest values in columns, in descending order. The columns that are not specified are returned as well, but not used for ordering.

data = pd.read_csv("employees.csv")
data.dropna(inplace = True)
large5 = data.nlargest(5, "Salary")
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

pandas.DataFrame.loc

A

Access a group of rows and columns by label(s) or a boolean array.

for lab, row in cars.iterrows():
    cars.loc[lab, "name"] = len(row["sex"])
df = pd.DataFrame([[1, 2], [4, 5], [7, 8]],
     index=['cobra', 'viper', 'sidewinder'],
     columns=['max_speed', 'shield'])

df.loc['cobra', 'shield']
👉 2
data = [[50, True], [40, False], [30, False]]
label_rows = ["Sally", "Mary", "John"]
label_cols = ["age", "qualified"]
df = pd.DataFrame(data, label_rows, label_cols)

print(df.loc["Mary", "age"])
👉 40
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

pandas.DataFrame.apply(func, axis=0, raw=False, result_type=None, args=(), **kwargs)

A

allows you to apply a function along one of the axis of the DataFrame, default 0, which is the index (row) axis.

cars["test"] = cars["sex"].apply(str.upper)
df = pd.DataFrame([[4, 9]] * 3, columns=['A', 'B'])
df
   A  B
0  4  9
1  4  9
2  4  9

df.apply(np.sqrt)
     A    B
0  2.0  3.0
1  2.0  3.0
2  2.0  3.0

df.apply(lambda x: [1, 2], axis=1)
0    [1, 2]
1    [1, 2]
2    [1, 2]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

pandas.DataFrame.tolist()

A

convert a series to list.

csv_file = 'data/phone_book.csv'
open_file = pd.read_csv(csv_file, delimiter=',')

file_to_list = []
file_to_list.append(open_file.columns.values.tolist())
file_to_list += open_file.to_numpy().tolist()

for row in file_to_list:
    print(row)

['first_name', 'last_name', 'phone_number']
['John', 'Lennon', 123]
['George', 'Harrisson', 456]
['Ringo', 'Starr', 789]
22
Q

pandas.DataFrame.values

A

property returns all values in the DataFrame. The return value is a 2-dimensional array with one array for each row.

df = pd.DataFrame({'age':    [ 3,  29],
                   'height': [94, 170],
                   'weight': [31, 115]})

df.values
array([[  3,  94,  31],
       [ 29, 170, 115]])
23
Q

pandas.DataFrame.to_numpy(dtype=None, copy=False, na_value=NoDefault.no_default)

A

Convert the DataFrame to a NumPy array.

pd.DataFrame({"A": [1, 2], "B": [3, 4]}).to_numpy()
array([[1, 3],
           [2, 4]])
df = pd.DataFrame({"A": [1, 2], "B": [3.0, 4.5]})
df.to_numpy()
array([[1. , 3. ],
            [2. , 4.5]])
24
Q

pandas.Series.tolist()

A

Return a list of the values.

data = {'Name':['Tony', 'Steve', 'Bruce', 'Peter' ], 'Age': [35, 70, 45, 20] } 
df = pd.DataFrame(data) 

df.values.tolist()
👉 [['Tony', 35], ['Steve', 70], ['Bruce', 45], ['Peter', 20]]
data = {'Name':['Tony', 'Steve', 'Bruce', 'Peter' ], 'Age': [35, 70, 45, 20] } 
df = pd.DataFrame(data) 
names = df['Name'].tolist()
 
print(names)
👉 ['Tony', 'Steve', 'Bruce', 'Peter']
data = {'Name':['Tony', 'Steve', 'Bruce', 'Peter' ], 'Age': [35, 70, 45, 20] }
df = pd.DataFrame(data) 
li = df.values.tolist()

print(li)
👉 [['Tony', 35], ['Steve', 70], ['Bruce', 45], ['Peter', 20]]
25
Q

seaborn.pointplot(data=None, *, x=None, y=None, hue=None, order=None, hue_order=None, estimator=’mean’, errorbar=(‘ci’, 95), n_boot=1000, units=None, seed=None, markers=’o’, linestyles=’-‘, dodge=False, join=True, scale=1, orient=None, color=None, palette=None, errwidth=None, ci=’deprecated’, capsize=None, label=None, ax=None)

A

Show point estimates and errors using dot marks.

data = sns.load_dataset("tips")
sns.pointplot(x = "sex", y = "total_bill", data = data)
plt.show()
data = sns.load_dataset("tips")
sns.pointplot(x = "sex", y = "total_bill", hue = "smoker", data = data)
plt.show()
data = sns.load_dataset("tips")

sns.pointplot(x = "size",
              y = "total_bill",
              linestyles = '-.',
              markers = '^',
              hue = "sex",
              data = data)
plt.show()
26
Q

matplotlib.step(x, y, *args, where=’pre’, data=None, **kwargs)

A

function designs the plot such that, it has a horizontal baseline to which the data points will be connected by vertical lines. This kind of plot is used to analyze at which points the change in Y-axis value has occurred exactly with respect to X-axis.

x = np.array([1, 3, 4, 5, 7])
y = np.array([1, 9, 16, 25, 49])
  
plt.step(x, y, 'g^', where='pre')
plt.show()
x = np.array([1, 3, 4, 5, 7])
y = np.array([1, 9, 16, 25, 49])
  
plt.step(x, y, 'r*', where='post')
plt.show()
27
Q

matplotlib.errorbar(x, y, yerr=None, xerr=None, fmt=’’, ecolor=None, elinewidth=None, capsize=None, barsabove=False, lolims=False, uplims=False, xlolims=False, xuplims=False, errorevery=1, capthick=None, *, data=None, **kwargs)

A

plot y versus x as lines and/or markers with attached errorbars.

xval = np.arange(0.1, 4, 0.5)
yval = np.exp(-xval)
  
plt.errorbar(xval, yval, xerr = 0.4, yerr = 0.5)
  
plt.title('matplotlib.pyplot.errorbar() function Example')
plt.show()
28
Q

matplotlib.barbs([X, Y], U, V, [C], **kwargs)

A

method is used to plot a 2D field of barbs. Barbs are used majorly in meteorology to plot the speed and direction of winds, but can be used to plot any two-dimensional vector quantity.

x = np.linspace(-5, 5, 5)
X, Y = np.meshgrid(x, x)
U, V = 12 * X, 12 * Y
  
data = [(-1.5, .5, -6, -6),
        (1, -1, -46, 46),
        (-3, -1, 11, -11),
        (1, 1.5, 80, 80),
        (0.5, 0.25, 25, 15),
        (-1.5, -0.5, -5, 40)]
  
data = np.array(data, dtype=[('x', np.float32),
                             ('y', np.float32),
                             ('u', np.float32), 
                             ('v', np.float32)])
  
plt.barbs(X, Y, U, V)
29
Q

matplotlib.eventplot(positions, orientation=’horizontal’, lineoffsets=1, linelengths=1, linewidths=None, colors=None, linestyles=’solid’, *, data=None, **kwargs)

A

used to plot identical lines at a given position. These plots, in general, are used for representing neural events in neuroscience, where more often it is called spike raster or dot raster or raster plot. More often it is also used for showing the timing or positioning of multiple sets.

positions = np.array([2, 4, 6])[:,np.newaxis]
offsets = [2,4,6]
  
plt.eventplot(positions, lineoffsets=offsets)
plt.show()
spike = 100*np.random.random(100)
plt.eventplot(spike, 
              orientation = 'vertical',
              linelengths = 0.8, 
              color = [(0.5,0.5,0.8)])
30
Q

matplotlib.hexbin(x, y, C=None, gridsize=100, bins=None, xscale=’linear’, yscale=’linear’, extent=None, cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, edgecolors=’face’, reduce_C_function=<function>, mincnt=None, marginals=False, *, data=None, **kwargs)</function>

A

module of matplotlib library is used to make a 2D hexagonal binning plot of points x, y.

np.random.seed(19680801)
    
n = 100000
x = np.random.standard_normal(n)
y = 12 * np.random.standard_normal(n)
     
plt.hexbin(x, y, gridsize = 50, cmap ='Greens')
plt.title('matplotlib.pyplot.hexbin() Example')
plt.show()
31
Q

matplotlib.get_cmap(name=None, lut=None)

A

Get a colormap instance, defaulting to rc values
if name is None.

32
Q

pandas.qcut(x, q, labels=None, retbins=False, precision=3, duplicates=’raise’)

A

Quantile-based discretization function. Tries to divide up the underlying data into equal sized bins.

df['quantile_ex_1'] = pd.qcut(df['ext price'], q=4)
df['quantile_ex_2'] = pd.qcut(df['ext price'], q=10, precision=0)
df['quantile_ex_4'] = pd.qcut(df['ext price'],
                            q=[0, .2, .4, .6, .8, 1],
                            labels=False,
                            precision=0)
33
Q

matplotlib.colorbar(mappable=None, cax=None, ax=None, **kwargs)

A

Add a colorbar to a plot.

cbar = colorbar()
cbar.solids.set_edgecolor("face")
draw()
fig, axes = plt.subplots(nrows=2, ncols=2)
  
for ax in axes.flat:
    im = ax.imshow(np.random.random((10, 10)), vmin=0, vmax=1)
  
plt.colorbar(im, ax=axes.ravel().tolist())
plt.show()
purchaseCount = [100, 200, 150, 23, 30, 50, 156, 32, 67, 89]
  
likes = [50, 70, 100, 10, 10, 34, 56, 18, 35, 45]
ratio = [1, 0.53, 2, 0.76, 0.5, 2.125, 0.56, 1.28, 1.09, 1.02]
  
plt.scatter(x=purchaseCount, y=likes, c=ratio, cmap="summer")
  
plt.colorbar(label="Like/Dislike Ratio", orientation="horizontal")
plt.show()
34
Q

numpy.Inf

A

floating point representation of (positive) infinity.

35
Q

seaborn.set_palette(palette, n_colors=None, desat=None, color_codes=False)

A

Set the matplotlib color cycle using a seaborn palette.

36
Q

seaborn.color_palette(palette=None, n_colors=None, desat=None, as_cmap=False)

A

can be used for coloring the plot. Using the palette we can generate the point with different colors.

current_palette = sns.color_palette()
sns.palplot(current_palette)
plt.show()
current_palette = sns.color_palette()
sns.palplot(sns.color_palette("Greys"))
plt.show()
current_palette = sns.color_palette()
sns.palplot(sns.color_palette("terrain_r", 7))
plt.show()
sns.palplot(sns.color_palette("dark", 10))
37
Q

seaborn.JointGrid(data=None, *, x=None, y=None, hue=None, height=6, ratio=5, space=0.2, palette=None, hue_order=None, hue_norm=None, dropna=False, xlim=None, ylim=None, marginal_ticks=False)

A

Grid for drawing a bivariate plot with marginal univariate(одномерный) plots.

penguins = sns.load_dataset("penguins")
sns.JointGrid(data=penguins, x="bill_length_mm", y="bill_depth_mm")
g = sns.JointGrid(data=penguins, x="bill_length_mm", y="bill_depth_mm")
g.plot(sns.scatterplot, sns.histplot)
g = sns.JointGrid(data=penguins, x="bill_length_mm", y="bill_depth_mm")
g.plot_joint(sns.scatterplot, s=100, alpha=.5)
g.plot_marginals(sns.histplot, kde=True)
sns.JointGrid(height=4, ratio=2, space=.05)
sns.JointGrid(xlim=(-2, 5), ylim=(0, 10))
38
Q

assert

A

это удобный способ вставить отладочные утверждения в программу. Этот оператор помогает обнаружить проблемы на ранних этапах программы, когда причина ясна, а не позже как побочный эффект какой-либо другой операции. Инструкция assert будет игнорироваться интерпретатором Python, если запустить его в оптимизированном режиме.

def password(string):
    if len(string) < 8: return False
    test_upper = any(x.isupper() for x in string)
    test_lower = any(x.islower() for x in string)
    test_digit = any(x.isdigit() for x in string)
    return all([test_upper, test_lower, test_digit])

if \_\_name\_\_ == "\_\_main\_\_":
    assert password("Abcd1234") == True
    assert password("Abcd123") == False
    assert password("abcd1234") == False
    assert password("AbcdefGhijKlmnopQRsTuvwxyZ1234567890") == True
    assert password("ABCD1234") == False
    assert password("Ab1!@#$%^&*()-_+={}[]|\:;?/>.<,") == True
    assert password("!@#$%^&*()-_+={}[]|\:;?/>.<,") == False
    assert password("") == False
    assert password(" aA1----") == True
    assert password("4aA1----") == True
x = "hello"
#if condition returns True, then nothing happens:
assert x == "hello"
#if condition returns False, AssertionError is raised:
assert x == "goodbye"

      assert x == "goodbye"
#	AssertionError
x = "hello"
#if condition returns False, AssertionError is raised:
assert x == "goodbye", "x should be 'hello'"

#	assert x == "goodbye", "x should be 'hello'"
#	AssertionError: x should be 'hello'
39
Q

pandas.DataFrame.to_json(path_or_buf=None, orient=None, date_format=None, double_precision=10, force_ascii=True, date_unit=’ms’, default_handler=None, lines=False, compression=’infer’, index=True, indent=None, storage_options=None)

A

Convert the object to a JSON string.

df = pd.DataFrame(
    [["a", "b"], ["c", "d"]],
    index=["row 1", "row 2"],
    columns=["col 1", "col 2"],
)
result = df.to_json(orient="split")
parsed = json.loads(result)
json.dumps(parsed, indent=4)  
{
    "columns": [
        "col 1",
        "col 2"
    ],
    "index": [
        "row 1",
        "row 2"
    ],
    "data": [
        [
            "a",
            "b"
        ],
        [
            "c",
            "d"
        ]
    ]
}
40
Q

pandas.DataFrame.iterrows()

A

method generates an iterator object of the DataFrame, allowing us to iterate each row in the DataFrame.

data = {
  "firstname": ["Sally", "Mary", "John"],
  "age": [50, 40, 30]
}

df = pd.DataFrame(data)
for index, row in df.iterrows():
  print(row["firstname"])

  Sally
  Mary
  John
41
Q

requests-html.links

A

Grab a list of all links on the page, as–is (anchors excluded)

42
Q

requests-html.absolute_links

A

Grab a list of all links on the page, in absolute form (anchors excluded)

43
Q

new

A

используется, когда нужно управлять процессом создания нового экземпляра, а __init__ – когда контролируется его инициализация. Поэтому new возвращает новый экземпляр класса, а init – ничего.

class Something:
    def \_\_new\_\_(cls, *args, **kwargs):
        print(f'конструируем: {args} | {kwargs}')
        instance = super().\_\_new\_\_(cls)

        # и вдруг нам захотелось добавить атрибут на лету
        instance.new_attribute = 'добавлено'
        print('почти готово')
        return instance

        def \_\_init\_\_(self, *args, **kwargs):
            print(f'инициализируем: {args} | {kwargs}')
            print(self.new_attribute)

my_obj = Something('раз', other=4)
👉 конструируем: ('раз',) | {'other': 4}
👉 почти готово
👉 инициализируем: ('раз',) | {'other': 4}
👉 добавлено

my_obj.new_attribute  # 'добавлено'
44
Q

id()

A

return the identity of an object.

str1 = "geek"
print(id(str1))
👉 140252505691448
str2 = "geek"
print(id(str2))
👉 140252505691448
print(id(str1) == id(str2))
👉 True
list1 = ["aakash", "priya", "abdul"]
print(id(list1[0]))
print(id(list1[2]))
👉 140252505691840
👉 140252505739928
print(id(list1[0])==id(list1[2]))
👉 False
45
Q

dir()

A

function returns all properties and methods of the specified object, without the values. This function will return all the properties and methods, even built-in properties which are default for all object.

class Person:
  name = "John"
  age = 36
  country = "Norway"

print(dir(Person))
['\_\_class\_\_', '\_\_delattr\_\_', '\_\_dict\_\_', '\_\_dir\_\_', '\_\_doc\_\_', '\_\_eq\_\_', '\_\_format\_\_', '\_\_ge\_\_', '\_\_getattribute\_\_', '\_\_gt\_\_', '\_\_hash\_\_', '\_\_init\_\_', '\_\_init_subclass\_\_', '\_\_le\_\_', '\_\_lt\_\_', '\_\_module\_\_', '\_\_ne\_\_', '\_\_new\_\_', '\_\_reduce\_\_', '\_\_reduce_ex\_\_', '\_\_repr\_\_', '\_\_setattr\_\_', '\_\_sizeof\_\_', '\_\_str\_\_', '\_\_subclasshook\_\_', '\_\_weakref\_\_', 'age', 'country', 'name']
test = "string"
print(dir(test))
['\_\_add\_\_', '\_\_class\_\_', '\_\_contains\_\_', '\_\_delattr\_\_', '\_\_dir\_\_', '\_\_doc\_\_', '\_\_eq\_\_', 
'\_\_format\_\_', '\_\_ge\_\_', '\_\_getattribute\_\_', '\_\_getitem\_\_', '\_\_getnewargs\_\_', '\_\_gt\_\_', '\_\_hash\_\_', 
'\_\_init\_\_', '\_\_init_subclass\_\_', '\_\_iter\_\_', '\_\_le\_\_', '\_\_len\_\_', '\_\_lt\_\_', '\_\_mod\_\_', '\_\_mul\_\_', 
'\_\_ne\_\_', '\_\_new\_\_', '\_\_reduce\_\_', '\_\_reduce_ex\_\_', '\_\_repr\_\_', '\_\_rmod\_\_', '\_\_rmul\_\_', 
'\_\_setattr\_\_', '\_\_sizeof\_\_', '\_\_str\_\_', '\_\_subclasshook\_\_', 'capitalize', 'casefold', 'center', 
'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 
'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 
'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 
'partition', 'removeprefix', 'removesuffix', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 
'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 
'translate', 'upper', 'zfill']
46
Q

getattr(object, name, default)

A

возвращает значение атрибута указанного объекта object по его имени name.

  • object - объект, значение атрибута которого требуется получить
  • name - имя атрибута объект, должно быть строкой
  • default - значение по умолчанию, которое будет возвращено, если имя атрибута name отсутствует.
a = [1, 2, 3, 4, 5]
c = ["pop", "append", 6]
getattr(a, c[0])()

print(a)
👉 [1, 2, 3, 4]

getattr(a, c[1])(c[2])
print(a)
👉 [1, 2, 3, 4, 5, 6]
class MyObj:
    name = 'Chuck Norris'
    phone = '+666111000'
    country = 'Norway'

x = getattr(MyObj, 'phone')
print(x)
👉 +666111000

Удалим атрибут phone из объекта MyObj:
delattr(MyObj, 'phone')

Пробуем получить атрибут phone из объекта MyObj:
x = getattr(MyObj, 'phone', '+600000000')
print(x)
👉 +600000000
47
Q

setattr(object, name, value)

A

устанавливает значение атрибута указанного объекта по его имени. Имя name должно быть именем существующего атрибута или будет создан новый атрибут.

  • object - объект, значение атрибута которого требуется установить,
  • name - имя атрибута, должно быть строкой,
  • value - произвольное значение атрибута.
class MyObj:
    name = 'Chuck Norris'
    phone = '+666111000'

setattr(MyObj, 'phone', '+600000000')
setattr(MyObj, 'country', 'Norway')

Получим атрибуты из объекта MyObj:
human = MyObj()

x = getattr(human, 'phone', '+600000000')
y = human.country

print(x, y)
👉 +600000000, Norway
class Person:
  name = "John"
  age = 36
  country = "Norway"

setattr(Person, 'age', 40)

The age property will now have the value: 40
x = getattr(Person, 'age')

print(x)
👉 40
48
Q

help()

A

is used to display the documentation of modules, functions, classes, keywords, etc.

help(print)

Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.
49
Q

pandas.DataFrame.ne(other, axis=’columns’, level=None)

A

method compares each value in a DataFrame to check if it is NOT equal to a specified value, or a value from a specified DataFrame objects, and returns a DataFrame with boolean True/False for each comparison.

df = pd.DataFrame([[10, 12, 2], [3, 4, 7]])
print(df.ne(7))

        0     1      2
  0  True  True   True
  1  True  True  False
50
Q

pandas.DataFrame.gt(other, axis=’columns’, level=None)

A

method compares each value in a DataFrame to check if it is greater than a specified value, or a value from a specified DataFrame objects, and returns a DataFrame with boolean True/False for each comparison.

df = pd.DataFrame([[10, 12, 2], [3, 4, 7]])
print(df.gt(7))

         0      1      2
  0   True   True  False
  1  False  False  False
51
Q

pandas.DataFrame.lt(other, axis=’columns’, level=None)

A

method compare each value in a DataFrame to check if it is less than a specified value and returns a DataFrame with boolean True/False for each comparison.

df = pd.DataFrame([[10, 12, 2], [3, 4, 7]])
print(df.lt(7))

         0      1      2
  0  False  False   True
  1   True   True  False
df = pd.DataFrame({'cost': [250, 150, 100],
                   'revenue': [100, 250, 300]},
                  index=['A', 'B', 'C'])

df == 100
    cost  revenue
A  False     True
B  False    False
C   True    False
52
Q

pandas.DataFrame.ge(other, axis=’columns’, level=None)

A

method compares each value in a DataFrame to check if it is greater than, or equal to a specified value, or a value from a specified DataFrame objects, and returns a DataFrame with boolean True/False for each comparison.

df = pd.DataFrame([[10, 12, 2], [3, 4, 7]])
print(df.ge(7))

         0      1      2
  0   True   True  False
  1  False  False   True