2 Flashcards

1
Q

Python: The lines within each block ( : ) must be

A

Indented e.g.
if var_1 == True:
print(“its true”)
break

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

Python: The function that places a value in the {} placeholder is

A

str.format(“value”) e.g. print(“my name is {}”.format(“Alen”))

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

Python: For a while loop, typing “while True:” will cause it to

A

loop forever, unless you add a “break” e.g.
if var_1 == 5
break

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

Python: For loops, the statement that will cause the loop to return to the top, without continuing the rest of the loop is called

A

continue

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

Python: To iterate/loop on every value in a list, use

A

for made_up_var in list_var:

print(made_up_var)

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

Python: Python stripts run in sequence so

A

Do not try to use a variable that only gets assigned later in the script

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

Python: For functions with arguments you must

A

Send in a value or else you will get an error

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

Python: An argument, with regards to functions, is

A

A value you pass into the function when you run it. e.g. my_function(argument1, argument2)

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

Python: You must tell a function what values to return by typing

A

return var_1, var_2

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

Python: functions start with the word

A

def

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

Python: function names should only use

A

lower case letters and underscores

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

Python: To add another if statement after the first one use

A

elif e.g.
elif var_1 == “SHOW”:
show_list()
continue

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

Python: After defining a function, to run it you must

A

call it e.g. my_function(argument)

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

Python: To import a python library, type

A

import name_of_library, into the python interpreter or top of script

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

Python: To call the “choice” method form the “random” library, type

A

random.choice()

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

Python: Can you create a new variable inside a function

A

Yes

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

Python: It’s recommended to do all the imports

A

at the beginning of the script

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

Python: It’s recommended to do the imports

A

at the beginning of the script

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

Python: You can imbed if and else commands

A

inside other if and else commands

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

Python: To get an automatic list of numbers starting from zero, type

A

list(range(10))

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

Python: You cannot reference variables outside of a function

A

From within the function. You must pass it in.

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

Python: To assign multiple variables simulaneously, type

A

var_1, var_2 = “Phil”, “Bill”

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

Python: Values on the right side of a variable always get

A

Evaluated first

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

Python: To insert a list into the middle of another list, type

A

list_1.insert(4, list_2)

The 4 denotes the index to insert at

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Python: To generate a range of numbers, possibly for a loop, type
range(10)
26
Python: You can concatenate two lists together, without placing the second list within one index, by typing
[1, 2, 3] + [4, 5]
27
Python: Should you add a colon : after calling, not defining, a function?
No
28
Python: To add a line to a string, type
\n
29
Python: To do arithmetic on a variable in a shorter way, type
var += 2 var -= 2 var *= 2 var /= 2
30
Python: To remove all white space from the beginning and end of a string, type
"My string".strip()
31
Python: Strings that are placed in a list must
have " " around them. e.g. | ["a", "b", "c"]
32
Python: Strings are
Immutable
33
Python: Lists are
Mutable
34
Python: To remove one item from a list by its index, type
del my_list[2]
35
Python: The del function does not work on
Strings, because they are immutable.
36
Python: To delete a list item by passing its value, type
my_list.remove(value)
37
Python: The remove() function only removes
The first instance of the passed value in the list
38
Python: When you use remove() on a value that does not exist it
throws an exception, so use within try/except
39
Python: To make a string lower case, type
lower("STRING") or "STRING".lower()
40
Python: To capitalize the first letter of a string, type
capitalize("string") or string.capitalize()
41
Python: To remove a value from a list by its index and return it, type
my_list.pop(2)
42
Python: Inputs, input(), that are numbers should
Be converted to int() because input() is a string by default
43
Python: To return a portion of a list of string, type
my_list[3:4] or "my string"[1:6]
44
Python: To slice until the end of a string without knowing its length, type
my_string[1:len(my_string)]
45
Python: Slices, [:], do not alter a list, they
Return a copy of it
46
Python: To slice a list or string by returning steps that skip, type
my_list[1::2], Add an extra colon
47
Python: To return a string or list backwards using slice, type
my_string[::-1], make the skipping step a negative, and swap the start and end range [10:1:-1]
48
Python: Slices that start a range as a negative
Move the start point backwards through the end of the string or list and start slicing from there.
49
Python: Standard format for a function
my_list = list(range(3)) ``` def first_4(my_iterable): four_arg = my_iterable[:4] return four_arg ``` first_4(my_list)
50
Python: Function checklist
The function def ends with : The function is called somewhere All of the arguments defined in the function are being passed in All of the lines are indented the same The variable referenced in the function are also assigned in the function It ends with return
51
Python: Can you delete from the middle of a string?
No
52
Python: To delete a slice, type
del my_list[1:3]
53
Python: To replace a slice of a list with new items, type
my_list[4:7] = ["e", "f"]
54
Python: The sections of the slice function are
[start:stop:step]
55
Python: To return the value associate with a key inside a dictionary, type
my_dict["key_name"]
56
Python: To create a dictionary, type
my_dict = {"Key": "Value", "Key2": "Value2"}
57
Python: You cannot return a key and value from a dictionary by its index because
The order changes and the keys and values are not attributed to an index
58
Python: Can you create a dictionary as a value of a key in another dictionary?
Yes
59
Python: Can you create lists within lists?
Yes
60
Python: The append() function is not recommended for concatenating 2 disparate lists because
It places the entirety of the appended list into the very last index of the initial string and does not set the values into individual indexes of the initial list.
61
Python: To return the value of a key in a dictionary that itself is the value of a key in a superceding dictionary, type
my_dict["key_name"]["key_name2"]
62
Python: Returning a value from inside a function does not print it, it
Turns the calling function into that value.
63
Python: Functions must end with
return
64
Python: Before saving new code always check presence of
All necessary colons and indentations.
65
Python: The format for a "for loop" that checks for the presence of each of a lists items in a dictionary and then adds the items that are present to another list, is
present_in_list = [] for item in my_list: if item in my_dict: present_in_list.extend(item)
66
Python: elif requires
a True value test in order to run
67
Python: else does not require
a test because it runs whenever the test on "if" was False
68
Python: Returning a value from inside a function does not print it, it
Turns the value of the of the calling function into the returned value. If more than one value it returns as a tuple.
69
Python: The boolean values True and False must be
capitalized.
70
Python: To fill placeholders in a string using the format method without knowing which order to put the values, you can assign key names to values by typing
"My name is {name_key} and I am {age_key} years old".format(age_key="22", name_key="Alen")
71
Python: To use the key name placeholders in the format method with the key values are stored in a dictionary, type
my_dict = {"state": "California", "name": "Alen"} | "I am {name} and I live in {state}".format(**my_dict)
72
Python: To create a new key for a dictionary, type
my_dict["new_key_name"] = "value"
73
Python: To change the value of an existing dictionary key name, type
my_dict["key_name"] = "new_value"
74
Python: To delete a key from a dictionary, type
del my_dict["key_name"]
75
Python: To add and change values of multiple dict keys at once, type
my_dict.update({"job": "Teacher", "age": "23", "gender": "male"})
76
Python: To create an empty dictionary, type
my_dict = {}
77
Pandas: To use pandas and be able to call it by a shorter name, type
import pandas as pd
78
Python: This returns a ``` def method(): return var_1, var_2, var_3 ``` method()
Tuple
79
Python: To return a tuple with the index and value from an iterable, type
enumerate(my_iterable)
80
Python: To unpack an enumerate()d tuple into a "for loop", type
for tuple in enumerate(my_iterable): print("This is item number {} and it is a {}".format(*tuple)) or for index, item in enumerate(my_iterable): print("This is item number {} and it is a {}".format(index, item)) or for tuple in enumerate(my_iterable): print("This is item number {} and it is a {}".format(tuple[1], tuple[2]))
81
Python: One asterisk in the format function
Unpacks a tuple
82
Python: To unpack an item tuple into a "for loop", type
for key, value in my_iterable.items(): | print("This is the key {} and the value is {}".format(key, value))
83
Python: You can create a tuple with either
placing a comma between 2 values or tuple()
84
Python: If a function returns three values you can either
Pack it into one variable or unpack it into three variable seperated by commas.
85
Python: To capitalize the first letter of every word, type
titlecase("my_string")
86
Python: To create a function that doesn't need to be passed every parameter because there are defaults, type
def my_function(param_1="A", param_2="B", param_3="Var"): my_function()
87
IPYNB: To launch the IPython Notebook from the console, type
ipython notebook
88
Python: To run a python script, type into the console
python3 ~/myfolder/script.py
89
Python: When using a "for loop" on a dictionay, the "item" variable only takes on the value of
The key, not the key value
90
Python: To use a "for loop" on a dictionary and have the "item variable" iterate on the dictionaries values instead of the keys, type
for key in my_dict.values(): print(key) or for key in my_dict: print(my_dict[key])
91
Python: To create a tuple, type
my_tuple = (1, 2, 3)
92
Python: For tuples, the parenthesis
Aren't required. Only the commas are required.
93
Python: A tuple is an
Immutable list that can be packed and unpacked.
94
Python: You can turn a list into a tuple by typing
my_tuple = tuple(my_iterable)
95
Python: To return the value at a certain index in a tuple, type
my_tuple[2]
96
To enter the python interpreter in the cosole, type
python
97
Python script file names end with
.py
98
to exit the python Interpreter you type
exit()
99
To exit the help(word) function, type
q
100
To look up the attributes and methods of a class use
dir(nameofclass)
101
To assign a user input to a variable, type
var_1 = input("Whatever you want the prompt to the user to be?")
102
To invoke a newer language you installed in the terminal type
e.g. python3
103
To create a new text file, type
nano new_name.py
104
The placeholder for the str.format() method is
{} e.g. "I'm {}, and you are {}".format("Alen", "Mike")
105
The if and else function lines must end with
: (also try and except)
106
The methods that come after the if and else statements must
Be indented the exact same amount. The amount of spaces or tabs doesn't matter.
107
A number with a decimal is called a
Float
108
A number without a decimal is called an
Integer
109
You can turn a string to an integer and a float with
int("55") float("2.2")
110
You can turn a float into an integer, and and integer into a float with
int(5.5) and float(5)
111
True + False will evaluate to
1 because True has a value of 1 and False has a value of 0
112
To check if a string is not in a variable string
if not "searchstring" in user_num: | print("not here")
113
To compare if two values are equal use
==
114
To try running something that might cause errors use
try: 1 / 0 except: print("script messed up")
115
You can check if a string is in another string or list by typing
"g" in "dog" | This would return True
116
To get more info on a function, type
Into the interpreter, help(str.split)
117
To return the length of a list, type
len(my_list)
118
To return an item in a list by its index, type
my_list[3]
119
To change the value of one index in a list, type
my_list[3] = "new value"
120
To seperate all the letters of a string into seperate list items
list("my string")
121
To seperate the words in a string by white space, type
my_string.split()
122
To join a list with a delimiter, type
"_".join(my_list)
123
The extend() method
Appends the second list onto the initial list and returns "None". The second list remains the same value but the initial list will now contain the additional list indexes.
124
Console: Servers usually do not have a
GUI
125
Console: The "~" in the command line stands for
The home directory e.g. users/student/
126
Console: Usually the first word in the command line is
The username you are signed in as
127
Console: To list the files in your current directory, type
ls
128
Console: To list the files in the current directory with more detail, like permissions, type
ls -l
129
Console: To list all the files in the current directory including the hidden dot files, type
ls -a
130
Console: To clear the screen, type
clear
131
Console: to list the files in another directory, type
ls user/student/folder/
132
Console: "Folders" is synonymous with
Directories
133
Console: To see your current directory, type
pwd
134
Console: The home directory ("~") usually contains the folders
My Documents, Pictures, etc.
135
Console: To change current directory, type
cd ~/myfolder/ or cd users/student/myfolder
136
Console: To move up one directory, type
..
137
Console: To see a previous command you typed, press
^ arrow
138
Console: To view the contents of a text file, type
less ~/myfolder/file.txt or cat ~/myfolder/file.txt
139
Console: To exit the "less" program, type
q
140
Console: To concatenate two disparate files, type
cat ~/myfolder/file.txt ~/myfolder/file2.txt
141
Console: To edit a text file, type
nano ~/myfolder/file.txt
142
Console: To use the menu at the bottom on nano you must hold
control
143
Console: To save in nano you must
ctrl x (exit) and then Y (yes) to the save prompt
144
Console: To Save As in nano you must
ctrl x (exit) and then Y (yes) to the save prompt, then change the name it prompts
145
Console: To rename a file or directory, type
mv hello.txt hi.txt or mv myfolder/ myfolder2/
146
Console: To refer to current directory in a path type
.
147
Console: When referencing directories always add
a slash at the end of the name
148
Console: To move and rename a file simultaneously, type
mv python.txt /users/student/myfolder/newname.txt
149
Console: To copy any file just type
cp myfolder/python.txt otherfolder/pythoncopy.txt
150
Console: To copy a directory with all of the files included in it to another location, type
cp -r ~/myfolder/ ~/myfolder2/
151
Console: To remove a file or an empty directory, use
rm myfolder/python.txt
152
Console: Be careful since there is no undo for
rm
153
Console: To use rm on a directory with files inside it, type
rm -r ~/myfolder/
154
Console: To create a directory, type
mkdir name_of_directory/
155
Console: To make a nested directory, type
mkdir -p documents/myfolder/pictures/
156
Console: The permissions in ls -l are ordered by
Creator, Group, Public
157
Python: Are tuples associated with an index?
Yes
158
Pandas: To give a name to a Series, type
my_series.name="Name of My Series"
159
Pandas: To give a name to an index, type
my_series.index.name="Name of My Index"
160
Math: The mean is also called the
Average
161
Math: The mode is
The number in a sequence that occurs most frequently
162
Math: The median is
The number that is in the middle of the sequence if you put them all in order.
163
Math: If you are attempting to find the median for a sequence has two numbers in the middle, you must
Find the mean for the two middle numbers
164
Pandas: The data in a Series is
Homogeneous. If you change one values from an Int to a float they all become float.
165
Pandas: If you use a dictionary as the data for a Series, it will
Automatically use the keys as the index and the values as the data.
166
Pandas: A Series is an
Ordered key-value store
167
Pandas: To multiply all the values in a Series, type
my_series * 2
168
Pandas: To return a slice of a Series by the label, type
my_series["Thur":"Sat"]
169
Pandas: To return a slice of a Series by the position, type
my_series[1:5]
170
Pandas: To return one value in a Series based on it's index, type
my_series[4]
171
Pandas: To set the value of one index in a Series, type
my_series[3] =188
172
Pandas: To return the median of a Series, type
my_series.median()
173
Pandas: To return the max of a Series, type
my_series.max()
174
Pandas: To change the values in a Series to the cumulative sum, type
my_series.cumsum()
175
Pandas: To return the values of a series enumerated, type
for idx, value in enumerate(my_series): print(idx, value) Note: The reverse doesn't work.
176
Pandas: To check if a key is in a series, type
"Tue" in my_series | Would return true or false
177
Pandas: To retrieve a Series value using a key or index, type
my_series["Tue"]
178
Pandas: To set a value by the key in a Series, type
my_series["Wed"] =22
179
Pandas: To loop over a Series or dictionary and return keys and values, type
for key, value in my_series.iteritems(): | print(key, value)
180
Python: When using my_list[4] to return a value, the square brackets contents can be
Anything that evaluates to a number. e.g. True, False
181
Python: To return the position of a string inside another string, type
"Look in my string for the position".find("my string")
182
Python: If the find() method cannot find the string inside the main string, it returns
-1
183
Python: The find() method only returns the position of
The first occurrence of the value you look for.
184
Python: You can make the find() method start searching only after a set position by typing
"Look in my string for the position".find("my string", 4)
185
Console: When writing a file or directory path, always start the path with
A slash
186
Python: To do exponentiation, type
2 ** 10
187
Python: To create a while loop function, type
``` def while_function: i = 0 while i ```
188
Python: To return the index of an item in a list by its value, type
my_list.index("list_item") | Note: If the item is not present, this returns an error.
189
Python: For value tests (like in while, if), an empty list and a not empty evaluate to
Empty: False | Not Empty: True
190
Internet: A network is a
Group of entities that can communicate even though they are not all directly connected.
191
Internet: Latency is
The time it takes for a message to go from source to destination.
192
Internet: Bandwidth is
The amount of information that can be transmitted per unit time.
193
IPYNB: To exit the ipython notebook from the console, type
control C, Y, Enter
194
Console: To see the route a site takes in the network to get to you and the time, type
traceroute www.google.com
195
Python: To time the execution of a function, type
import time ``` def time_execution(): start = time.clock() eval("25 * 25") stop = time.clock() execution = stop - start return execution ```
196
Python: Another way to execute the evaluation of code is
eval(2 * 2)
197
Math: Modulo (%) is the
Remainder after dividing. e.g 5 % 2 = 1
198
Python: To return a number associated with a single letter, type
ord("A")
199
Python: To return a letter associated with a single number, type
chr(114)
200
Pandas: A pandas DataFrame is
A spreadsheet with row and column labels.
201
Pandas: A DataFrames data in a column is
Homogeneous
202
Pandas: A DataFrames data can
Be any type
203
Pandas: To create a DataFrame, type
my_data_dict = {"Tokyo": [23, 43, 12, 65], "London": [3, 4, 27, 55], "Date": [1/20, 1/21, 1/22, 1/23]} pandas.DataFrame(my_data_dict)
204
Pandas: Every key-value list in the data dictionary going into a DataFrame must
Be the same length.
205
Pandas: To return one column in a DataFrame (as a Series), type
df["column name"] or | df.column_name
206
Pandas: The data type for the return of one column in a DataFrame is
a Series
207
Pandas: To set a DataFrame column to be the index, type
df.set_index("column name") | Note: Must use quotes.
208
Pandas: To return only that last n rows of a DataFrame, type
my_dataframe.tail(10)
209
Pandas: To return all unique values from a column along with their frequency, type
my_dataframe["Column Name"].value_counts() or | my_dataframe.Column.value_counts()
210
Pandas: To create a new column that totals others, type
df["total"] = df["Column1"] + df["Column2"] + df["Column3"]
211
Plarium: The Tracker counts the ROI up until
The current moment for all the regs within the chosen time period.
212
Plarium: The Payments ROI Comparison tool counts ROI until
The end date that you set at midnight.
213
Plarium: For the end of month campaigns report
Use the Payments ROI Comparison tool.
214
Pandas: To use value_counts(), you must use it on
A column Series
215
Pandas: When importing a csv, you do not need to type
DataFrame
216
Pandas: To sort a DataFrame by the index, type
df.sort_index()
217
Pandas: To sort the columns of a DataFrame, type
df.sort_index(axis=1)
218
Pandas: When referring to a column make sure to
Spell it perfectly and see if it needs quotes.
219
Python: To open a browser automatically to a certain site from python, type
import webbrowser webbrowser. get('firefox') webbrowser. open("http://www.google.com")
220
Pandas: To sort a DataFrame by a column's values, type
df.sort_index(ascending=False, by=["Converted clicks"])
221
Pandas: To sort a DataFrame by a two column's values, type
df.sort_index(ascending=[False,True], by=["Converted clicks", "Avg. position"])
222
Pandas: The order method returns a
Series
223
Pandas: To sort a DataFrame you must use the
sort_index() method
224
Chrome: To open last closed tab, type
Ctrl, Shift, T
225
Python: Before calling new variables, make sure to
Initialize them. | eg. my_var = 0
226
Pandas: The groupby object is not a
DataFrame. It is a dictionary where each unique value is a key and the value is the dataframe that has that value attributed.
227
Python: The print function must use
Parentheses
228
Pandas: The groupby() function
Splits the DataFrame into separate dataframe objects for every unique value in the passed in column.
229
Pandas: A groupby object is dict like because (2 reasons)
1. The unique column values are keys and the rest of the values are key values. 2. It is iterable.
230
Pandas: To create an empty DataFrame, type
df = pandas.DataFrame()
231
Numpy: To invoke pylab, type
%pylab inline
232
Pandas: To create a date range, type
days = pandas.date_range("2014-01-01", "2014-02-28", freq = "d")
233
Pandas: To slice out a small range of the rows and columns, type
df.ix[2:45, "Madrid":"Boston"]
234
Pandas: To slice out specific rows and specific columns, type
df.ix[[5, 22, 31] , ["Madrid","Boston","Shanghai"]]
235
Pandas: To slice out specific rows and all columns, type
df.ix[[5, 22, 31] , : ]
236
Pandas: To slice out all rows and a range of columns, type
df.ix[ : , ["Madrid":"Boston"]]
237
Pandas: To transpose the columns and rows of a DataFrame, type
new_df = df.T
238
Pandas: To look at just a few of the columns in a DataFrame, type
new_df = df[["Bangladesh, "India", "Uganda"]]
239
Time: To make a python script wait for a certain time, type
import time | time.sleep(60)
240
Console: To see the running processes, type
top -o cpu
241
Console: To exit the "top" , type
q
242
Pandas: When writing a path starting with the home directory, you must
Begin it with a slash.
243
Pandas: Pandas automatically removes any excess
Blank rows and column from the CSV.
244
Pandas: To check the number of rows in your DataFrame, type
len(df.index)
245
Pandas: To change one column name, type
df = df.rename(columns = {"old_name": | "new_name"})
246
Pandas: To set a default maximum number of rows to display, type
pandas.set_option("display.max_rows", 10)
247
Pandas: In order for df.to_csv("") to work, df must be
a DataFrame or a Series
248
Pandas: In order to filter in pandas you must first create a
mask
249
Pandas: To apply your mask to your df (filter for), type
df[mask]
250
Pandas: To apply the inverse of your mask (filter out), type
df[numpy.invert(mask)] | Note: Must import numpy for this.
251
Pandas: When you apply a boolean index (mask) to a df, it only returns the rows that the boolean index had as
True
252
Pandas: To create a mask for one criteria, type
df["Ad group"]=="Banner" or | df.Cost
253
Pandas: To create a mask for multiple criteria, type
mask = (df["Ad group"]=="Banner") & (df.Cost>200)
254
Matplotlib: To plot a line graph based on two df columns, type
df.plot(x="Campaign", y="Cost")
255
Pandas: To get stats on every df column at a glance, type
df.describe()
256
Pandas: To get a stat (e.g. median) for one column, type
df["Column Name"].median()
257
Pandas: To create a groupby, type
df.groupby("Column Name")
258
Pandas: To remove the first row of a df, type
df = df.drop(df.index[:1])
259
Pandas: To change the value of an exact coordinate (row and column), type
df.loc[5, "Cost"] = 10
260
Pandas: To return the value of an exact coordinate (row and column), type
df.loc[24, "Campaign"]
261
Python: To pass a function value to the third parameter, while leaving the others as their defaults, type
def my_function(param_1="A", param_2="B", param_3="Var"): my_function(param_3="New Value!")
262
Python: Can you add an equation to the return line at the end of a function?
Yes
263
Python: To apply a predefined function to every item in a list, in a short way, type
map(my_function, my_list)
264
Pandas: To remove the last row of a df, type
df = df.drop(df.index[-1:])
265
Pandas: To create a function meant to return a boolean index to be used as a filter(), type
``` new_list = [] def my_filter(a_list): for item in a_list: test = item > 5 new_list.append(test) return new_list ```
266
Pandas: To create a pivot table and choose the rows, columns, values, and presence of grand totals, type
table = pandas.pivot_table(df,index=["Manager","Status"],columns=["Product"],values=["Quantity","Price"],aggfunc={"Quantity":len,"Price":[numpy.sum,numpy.mean]},fill_value=0)
267
Pandas: To create a new column and make the values the return of a function acting on another column, type
df["New Column"] = df["Old Column"].apply(function_name) | Will be useful for data cleanup
268
Python: To use a list comprehension to alter the items in a list, type
[float(item) for item in my_list]
269
Pandas: To create a lambda function, type
lambda var_name: var_name**2
270
Pandas: To create a list comprehension with an if statement, type
[item for item in my_list if item>5]
271
Pandas: Can a lambda function take in multiple values?
Yes. | e.g. lambda var_name, var_name2: var_name*var_name2
272
Pandas: Lambda functions automatically returns
The of the evaluation after the colon
273
Numpy: The method that returns a standard deviation is
my_numpy_array.std()
274
Pandas: To reverse the order of all the rows in a df, type
df.ix[::-1]
275
Pandas: To combine two tables based on similar values in one column, like a lookup table, type
df.merge(df2, on="Similar_Column_name")
276
Pandas: For a merge() to work the columns that the tables will merge on must be
Labeled the same.
277
Pandas: Values (in the column that two tables are merging on) that do not match exactly on both tables when merging are
Removed
278
Pandas: If a value in the merged on column of one table has a duplicate value that is also an exact match in the other table
The duplicate gets included in the merged table.
279
Pandas: When the merged column has 2 duplicate exact match values on both tables, it
Combines the rows in every possible configurations, because it does not know which of the duplicates on one table matches with which of the duplicates of the other.
280
Excel: To switch 2 rows or cells
Select the cell or row and press Ctrl x, then select the destination cell or row and press Ctrl, Shift, =
281
Pandas: To replace an exact cell value in a specified entire column of a df with another value, type
df["Column 1"] = df["Column 1"].replace({"Fee":"Fee Time"})
282
Pandas: To strip all "$" signs from one column, type
df["Column 1"] = df["Column 1"].apply(lambda x: x.strip("$"))
283
Pandas: To strip any arrangement of a list of characters from the beginning of a string in a column, type
df["Column 1"] = df["Column 1"].map(lambda x: x.lstrip("-+=*&")) or df["Column 1"] = df["Column 1"].lstr.strip("-+=*&")
284
Excel: To calculate correlation between 2 arrays, type
=correl(array_1, array_2)
285
Numpy: To filter a numpy array with a boolean index, type
numpy_array[numpy_array > 5]
286
Numpy: To use two boolean index filters on a numpy array, type
numpy_array[(numpy_array==5) | (numpy_array > 6)]
287
Numpy: To create a numpy array, type
numpy.array([1,2,3])
288
Numpy: To get the position of the max value in a numpy array, type
numpy_array.argmax()
289
Numpy: To create a range in a numpy array, type
numpy.arange(20,30,1) | Start,stop,step.
290
Numpy: Nan values in a numpy array
Screw up calculations and must be dealt with beforehand.
291
Requests: To scrape all of the html content of a page into a variable, type
import requests page = requests.get("http://www.scrape.com")
292
Requests: To see all of the html scraped by requests in your variable, type
page.content
293
BS4: To put scraped data into BS from the var used by requests, type
from bs4 import BeautifulSoup | soup_page = BeautifulSoup(page.content, "html.parser")
294
BS4: To print a BS content variable in a pretty way, type
print(soup_page.prettify())
295
BS4: To return all of the content contained in a certain html tag, type
soup_page = BeautifulSoup(page.content) soup_page.find_all("a")
296
BS4: To return all the values of a certain parameter from a list of html tags, type
for item in soup_page.find_all("a"): | print(item.get("href"))
297
BS4: To return all the anchor text for every link in a soup page, type
for item in soup_page.find_all("a") | print(item.text)
298
Python: To return the value of a dictionary key using a method, type
my_dict.get("Key name")
299
Requests: The requests library is a
site scraper
300
BS4: The beautiful soup is an
html parser
301
Pandas: To add new rows to a DataFrame, use
concat
302
Pandas: To add new columns to a DataFrame that are a different length than the original and have pandas fill the the missing data with nan, rather than deleting any rows, type
df1.join(df2, how='outer')
303
Pandas: To groupby by two columns, type
grouped = df.groupby(["Google Name", "Adgroup"]).agg({"Regs": numpy.sum, "Deposits Amount": numpy.sum})
304
Pandas: To turn a column value that pandas thinks is an int/float to a string, type
df["Campaign"] = df["Campaign"].apply(lambda x: str(x))
305
Pandas: To turn a groupby into a flat table, type
grouped.reset_index()
306
Pandas: To remove all rows with "VALUE!", inside one column of a data frame, use
A filter.
307
Pandas: To remove rows with duplicate values in a df column and keep the last one, type
df.drop_duplicates(subset="Column name", take_last=True)
308
Pandas: To filter for df rows that contain certain values in a column, type
df[df['Column Name'].isin([3, 6])]
309
Pandas: To create a filter mask with "or" criteria, type
(df["Column 1"] >= 5) | (df["Column 2"] > 45)
310
Pandas: To create a groupby for multiple columns data, and aggregate one of the columns by two functions, type
df.groupby(["Campaign", "Adgroup"]).agg({"Adgroup": [numpy.size, numpy.mean]})
311
Pandas: The apply function does not require your
function to iterate.
312
Pandas: To return the row of a certain value in a column, type
df["Column_name"][df["Column name"] == "Value name"].index.tolist()[0]
313
Pandas: To turn a list into a number, use
"".join("my_list")
314
BS4: The find_all("a") method returns
a List of all the "a" tags.
315
Pandas: To use a text filter on a df, type
df[df["Column name"].str.contains("string")]
316
OS: To return the current directory automatically, type
import os path = os.getcwd()
317
BS4: To parse a site with potentially broken html, type
soup_page = BeautifulSoup(page, "html.parser")
318
BS4: To use .get("href") and then place return into a list, type
new_list = [] for item in soup_page.find_all("a", {"class":"title may-blank "}: new_list.extend([item.get("href")])
319
BS4: When using soup_page.find_all("a", {"class":"name"})
The parameters must be an exact match
320
Selenium: To run a loop that will sometimes return errors, but you want it to continue, type
``` for item in site_list: try: my_browser.get(item) except: pass ```
321
Python: To execute some code immediately after a for loop has iterated over every item, type
for item in range(1,10): print(item) else: print("finished")
322
CLI stands for
command line interface
323
python: Python's GIL is
a Global Interpreter Lock. A mechanism that prevents executions of multiple python bytecode instructions simultaneously.
324
http: SSL stands for
secure sockets layer
325
http: SSL is
an encrypted connection between server and client
326
bash: To remove a directory type
rm -r directory_name
327
python: When importing a file,
point to it from your current working direcory
328
python: Do not name your python file
the same as a library name you are importing, otherwise it will import itself.
329
python: To evaluate a string that is assigning a value to
exec("var_name = 1")
330
python: var_name = 1 is not an expression, it is a
statement
331
python: To return the first value in a list that matches a criteria, type
first(x for x in my_list if x == 10)
332
pandas: To import mongodb into pandas, use
pymongo
333
python: To sort a dict by key, type
sorted(my_dict.items(), key=lambda x: x)
334
python: my_dict.items() returns
a list where each item is a tuple that contains the key and value
335
python: To remove an df from RAM memory, type
del df import gc gc.collect()