PythonBasics Flashcards

1
Q

print working directory

A

pwd

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

my computer’s network name

A

hostname

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

make directory

A

mkdir

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

change directory

A

cd

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

list directory

A

ls

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

what should you do if you get lost in terminal?

A

write “pwd” to print working directory and next “cd ~ ” to get back to home

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

How do you create a folder with multiple words in the name?

A

write mkdir for creating a folder and then put “” around the name. For example,…. Mkdir “that’s pretty cool”.

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

remove directory

A

rmdir

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

What does “cd ..” do?

A

it takes you back one folder from for example /desktop/temp/stuff to /desktop/temp, This can also be done for multiple folders back with ../../.. (3 folders back)

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

What does CLI mean?

A

Command line interface

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

What does GUI mean?

A

graphical user interface

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

What will “ls -1R” do?

A

it will show you an overview of all folders below your current position.

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

push directory

A

pushd (go to new location)

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

pop directory

A

popd (return to saved location)

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

what does “mkdir -p” do?

A

lets you create an entire directory at once aka. Multiple folders in a tree. Mkdir -p kap/jokap/chakap/ja/sådan/er/det

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

copy a file or directory

A

cp

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

move a file or directory

A

mv

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

What can be the problem if you are not allowed to rmdir a directory?

A

First of all there could be files within that folder you try to delete. Secondly, if you are 100 % sure that there is nothing, it may be due to the Mac OSX issue of an “.DS_Store” file, which you can delete by going to the folder and write “rm -rf .DS_Store”

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

page through a file

A

less

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

print the whole file

A

cat

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

What will
mkdir something
cp awesome.txt something/
Do?

A

first we create a new folder/directory. Next we make a copy of the file awesome.txt from current pwd to this new folder called “something”

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

create a new file

A

touch (write…. “touch iamcool.txt” for example)

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

How do we copy a file?

A

go to the directory of the file and write “cp filename.txt newfilesname.text”

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

execute arguments

A

xargs

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

find files

A

find

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

how do you rename a file?

A

use mv (move a file) so you write “mv filename.doc newfilename.doc”. you can check the name change after by listing the directory content with ls

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

how can you see the content of a file?

A

write “less filename.txt” and it will show you the content. To quit, just type q

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

How can you remove a FILE?

A

just write “rm filename.doc” (you can also remove multiple files at once by listing them)

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

How can you remove a folder/directory?

A

rmdir foldername

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

How can you delete a file within a folder?

A

rm foldername/filename.txt

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

How can you delete a folder that contains files?

A

rm -rf foldername

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

Whats the “execute” command?

A

xargs

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

find things inside files

A

grep

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

read a manual page

A

man

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

find what man page is appropriate

A

apropos

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

look at your environment

A

env

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

print some arguments

A

echo

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

export/set a new environment variable

A

export

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

exit the shell

A

exit

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

What is the command to become a super-user?

A

sudo makes you a super-user and lets you force through more commands

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

Which command can you use to change the access permissions to a file?

A

chmod

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

How can you change the file owner?

A

use chown

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

How do you run a python file from terminal?

A

Just write “python filename.py”

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

What does # do in python and what is it called?

A

A hash.

If you put a hash (#) in front of a line of code, it will not print / go in effect so you can use it for making comments

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

How can you print a hash/octothorpe? “#”

A

you put it inside strings; print “we have a # inside of the strings now” # the hash inside the strings will be printed but this comment after the second # will not be printed.

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

What is / called and what is it?

A

slash and it is the division sign

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

What is * called and what is it?

A

asterisk and it is the multiplier sign

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

How do you make additions?

A

with “+” for example “print 3 + 2” will output 5

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

How do you make subtractions?

A

with “-” for example “print 3 - 4” will output -1

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

What is “%” and what does it do?

A

percent and it works as ‘X divided by Y with J remaining’. For example, “100 divided by 16 with 4 remaining” so print “100 % 16” will be 4. While 100 % 10 will be 0.

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

What are floating point number?

A

Essentially just numbers that include decimals such as 5.5 or 0.02

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

What output will print “20/3” give?

A

6… if you want the precise output including decimals you need to write print “20.0/3.0) and you will get 6.666666666666667

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

how do you write less-than-equal?

A

<=

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

how do you write greater-than-equal?

A

>=

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

What is the order of mathematical operations?

A

PEMDAS, Parentheses, exponents, multiplication, division, addition, subtraction

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

How do you create a variable in python?

A

you basically just write out with a “=” sign in-between so for example,

“Cars = 100” # creates a variable that ‘cars’ is equal to 100

“cars_not_driven = cars – drivers” # creates a variable for available cars (cars_not_driven)

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

What is the difference between = (single-equal) and == (double-equal)?

A

The = (single-equal) assigns the value on the right to a variable on the left. The == (double-equal) tests if two things have the same value.

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

Can we write x=100 instead of x = 100?


A

You can, but it’s bad form. You should add space around operators like this so that it’s easier to read.

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

How do you define number and character variables?

A

Examples…

My_name = “Lars Horsbol”

And

My_age = 23

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

How do you insert variables inside printed text?

A

you use %s for character-variables and %d for number variables. For example,

Print “Let’s talk about %s” % my_name

And

Print “He’s %d centimeters tall.” % my_age

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

How should you refer to a number-variable inside printed text?

A

With %d

Print “He’s %d centimeters tall.” % my_age

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

How should you refer to a character-variable inside printed text?

A

With %s

Print “Let’s talk about %s” % my_name

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

How do you add variables together inside printed text?

A

You just write them out in the %d.

print “If I add %d, %d, and %d I get %d.” % (my_age, my_height, my_weight, my_age + my_height + my_weight)

here we are trying to add the variables together

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

How does the %r format work?

A

If you use %r, it will print no matter what, meaning that it will both print a character-variable (normally %s) and a number-variable (normally %d).

print “and with both character %r AND number %r” % (height, hair) # with %r we will end up printing both the character-variable AND the number-variable

and with both character 192 AND number ‘Dark-blond’

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

What are the different conversion types in Python and which ones are most common?

A

%d = signed integer decimal

%s = character-variables

%r = ”print this no matter what”

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

What’s the point of %s and %d when you can just use %r?


A

The %r is best for debugging, and the other formats are for actually displaying variables to users.

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

Can I make a variable like this: 1 = ‘Zed Shaw’?

A

No, the 1 is not a valid variable name. They need to start with a character, so a1 would work, but 1 will not.

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

How can we insert a number inside “x = “There are %d types of people” ?

A

We write “% 10”

x = “There are %d types of people.” % 10 # here we write a string and insert a variable with % outside the string

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

How can you combine two strings such as:

w = “this is the left side of…“

e = “a string with a right side.” ??

A

just write; print w + e

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

How can I round a floating point number?

A

You can use the round() function like this: round(1.7333) or round(weight / pounds_to_kilo)

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

Can I use single-quotes or double-quotes to make a string or do they do different things?

A

In Python either way to make a string is acceptable, although typically you’ll use single-quotes for any short strings like ‘a’ or ‘snow’ and double-quotes for sentences.

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

Should I use %s or %r for formatting?


A

You should use %s and only use %r for getting debugging information about something. The %r will give you the “raw programmer’s” version of variable, also known as the “representation.”

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

Why do you put (single-quotes) around some strings and not others?

A

Mostly it’s because of style, but I’ll use a single-quote inside a string that has double-quotes. Look at line 10 to see how I’m doing that.

print “I also said: ‘%s’.” % y

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

What will; print “.” * 10 do?

A

this will result in 10*. So the output will be; ……….

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

If, end1 = “C”

end2 = “h”

end3 = “e”

end4 = “e”

end5 = “s”

end6 = “e”

end7 = “b”

end8 = “u”

end9 = “r”

end10 = “g”

end11 = “e”

end12 = “r”,

what will;

print end1 + end2 + end3 + end4 + end5 + end6

print end7 + end8 + end9 + end10 + end11 + end12

result in?

A

Just.

Cheese

burger

On two different lines because there is no comma after “end6”.

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

Why do I have to put quotes around “one” but not around True or False?

A

That’s because Python recognizes True and False as keywords representing the concept of true and false. If you put quotes around them, then they are turned into strings and won’t work right.

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

If, end1 = “C”

end2 = “h”

end3 = “e”

end4 = “e”

end5 = “s”

end6 = “e”

end7 = “b”

end8 = “u”

end9 = “r”

end10 = “g”

end11 = “e”

end12 = “r”,

what will;

print end1 + end2 + end3 + end4 + end5 + end6,

print end7 + end8 + end9 + end10 + end11 + end12

result in?

A

Just.

Cheese burger

On two different lines because there is a comma after “end6” it stays on the same line in the printed output.

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

What will; print “Its fleece was white as %s.” % ‘snow’ result in?

A

Its fleece was white as snow.

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

If you have; formatter = “%r %r %r %r” , what will happen if you write print formatter % (formatter, formatter, formatter, formatter)

A

‘%r %r %r %r’ ‘%r %r %r %r’ ‘%r %r %r %r’ ‘%r %r %r %r’

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

If you have; formatter = “%r %r %r %r” , what will happen if you write;

print formatter % (

“I had this thing.”,

“That you could type up right.”,

“But it didn’t sing.”,

“So I said goodnight.”

)

A

‘I had this thing.’ ‘That you could type up right.’ “But it didn’t sing.” ‘So I said goodnight.’

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

Why do I have to put quotes around “one” but not around True or False?

A

That’s because Python recognizes True and False as keywords representing the concept of true and false. If you put quotes around them, then they are turned into strings and won’t work right.

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

I tried putting Chinese (or some other non-ASCII characters) into these strings, but %r prints out weird symbols.


A

Use %s to print that instead and it’ll work.

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

Why does %r sometimes print things with single-quotes when I wrote them with double-quotes?

A

Python is going to print the strings in the most efficient way it can, not replicate exactly the way you wrote them. This is perfectly fine since %r is used for debugging and inspection, so it’s not necessary that it be pretty.

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

Why do the \n newlines not work when I use %r?


A

That’s how %r formatting works; it prints it the way you wrote it (or close to it). It’s the “raw” format for debugging.

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

What are two ways to make a string that goes across multiple lines?

A
  1. You can use \n è “nJan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug”
  2. You can use a triple double quote “”” ………… “””
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
86
Q

What will making three double-quotes allow you to do? “”” “””

A

Write a lot of text within the “”” here “”” on multiple different lines

print “””

There’s something going on here.

With the three double-quotes.

We’ll be able to type as much as we like.

Even 4 lines if we want, or 5, or 6.

”””

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

How do you write some text that is tabbed in?

Like this?

A

You use; \t so for example

Print “\tI’m tabbed in.”

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

How do you write a string of text and then split the text on different lines?

A

You use \n so for example…

Print “I’m split\non a line.”

Will become;

I’m split

On a line

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

What will; print “I’m \ a \ cat.” ; result in?

A

I’m \ a \ cat

So it removes one of the backlashes

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

How can you produce a list of points?

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

What does the escape sequences \ do?

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

What does the escape sequence \’ do?

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

What does the escape sequence; \a do?

A

It inserts an extra space.

So;

Print “hey man\a whats up”

Becomes

Hey man whats up

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

What does the escape sequence \b do?

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

What does the escape sequence \n do?

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

What does the escape sequence \r do?

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

What does the escape sequence \t do?

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

When I use a %r format none of the escape sequences work.

A

That’s because %r is printing out the raw representation of what you typed, which is going to include the original escape sequences. Use %s instead. Always remember this: %r is for debugging; %s is for displaying.

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

What is the function of the comma in this:

print “How old are you?”,

age = raw_input()

A

the function of the comma is to make sure that the output is on the same line.

With comma we get; How old are you? 23

Without comma we get: How old are you?

23

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

What is raw_input in python and what is it useful for?

A

Raw_input lets you interact with the outside world to get input. The raw_input() function waits for the user to type some input and press return. It then gets whatever was typed.

(called input() in python 3 but here it also tries to convert the input whereas rawinput from python 2 simply takes the raw input which is usually easier to work with/safer)

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

How can you insert prompts for raw_input requests?

A

Just write questions or prompts inside the string of the raw_input() like this;

height = raw_input(“How tall are you? Fx. 1.82 m”)

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

How can you look up what any function in python does from within the terminal?

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

Why would I use %r over %s?

A

Remember, %r is for debugging and is “raw representation” while %s is for display. I will not answer this question again, so you must memorize this fact. This is the #1 thing people ask repeat- edly, and asking the same question over and over means you aren’t taking the time to memorize what you should. Stop now, and finally memorize this fact.

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

What do you also call a “.py” file?

A

A script

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

What are modules?

A

Modules are features you can add in your python script. Some people also called modules for libraries.

It could for example be: sys and argv

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

Python filename.py arg1 arg2 arg3

So for example

Python e13.py I want snacks

Will give…

The script is called: e13.py

Your first variable is: i

Your second variable is: want

Your third variable is: snacks

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

What happens if you input less arguments for argv when trying to run in the command line?

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

What’s the difference between argv and raw_input()?

A

The difference has to do with where the user is required to give input. If they give your script inputs on the command line, then you use argv. If you want them to input using the keyboard while the script is running, then use raw_input().

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

How can you use raw_input prompts for multiple places in your script?

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

Does txt = open(filename) return the contents of the file?

A

No, it doesn’t. It actually makes something called a “file object.” You can think of it like an old tape drive that you saw on mainframe computers in the 1950s or even like a DVD player from today. You can move around inside them, and then “read” them, but the file is not the contents

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

What does from sys import argv mean?

A

For now, just understand that sys is a package, and this phrase just says to get the argv feature from that package. You’ll learn more about these later.

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

Why is there no error when we open the file twice?

A

Python will not restrict you from opening a file more than once, and in fact sometimes this is necessary.

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

Which command will let you close files?

A

Close

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

Which command will let you read files?

A

Read

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

Which command will let you open files?

A

Open

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

Which command will let you read just one line?

A

Readline

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

Which command will let you empty a file for its content? (be careful)

A

Truncate

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

Which command will let you write stuff into your file?

A

Write(stuff)

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

What could we write to open a file? (existing or not)

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

What could we write to open and read a file?

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

What does the file modes “r” do in python?

A

This is the default mode. It Opens file for reading.

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

What does the file modes “w” do in python?

A

This Mode Opens file for writing.
If file does not exist, it creates a new file.
If file exists it truncates the file.

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

What does the file modes “x” do in python?

A

Creates a new file. If file already exists, the operation fails.

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

What does the file modes “a” do in python?

A

Open file in append mode.
If file does not exist, it creates a new file.

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

What does the file modes “t” do in python?

A

This is the default mode. It opens in text mode.

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

What does the file modes “b” do in python?

A

This opens in binary mode.

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

What does the file modes “+” do in python?

A

This will open a file for reading and writing (updating)

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

How can you write a simple script that will open a file specified in command/terminal?

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

If you open the file with ‘w’ mode, then do you really need the target.truncate()?

A

No. It doesn’t make any difference as the “w-mode” will overwrite the existing file so emptying that file first makes no difference.

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

Does just doing open(filename) open it in ‘r’ (read) mode?

A

Yes, that’s the default for the open() function.

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

What can the “import” statement do?

A

With the import statement we can import new modules to python.

Some modules/functions are existing in your python program already, but sometimes you may want to add new functions. Here, it can be smart to import code written by others than to innovate yourself. You can import such new modules with the import statement.

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

Why do you have to do output.close() in the code when the file is already closed?

A

It is good practice to always close files so you avoid later commands working with those files from reading descriptions from those closed files.

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

What does the len() function do?

A

It gets the length of the string that you pass to it and then returns that as a number. Play with it.

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

What do functions do? (put simply)

A
  1. They name pieces of code the way variables name strings and numbers.
  2. They take arguments the way your scripts take argv.
  3. Using #1 and #2, they let you make your own “mini-scripts” or “tiny commands.”

Think of “function” as a mini-script

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

How do you create a function in python?

A

Write; def (for define)

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

The first function is overly complicated. It takes the look of when we work with args and does uses *args inside print_two. On the line below, it unpacks those args to finally print it.

There is no need to do this unpacking. We can simply put arg1, arg2 inside the function from the beginning.

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

How should you start a function?

A
  1. With; def (for definition)
  2. And then open paranteheses right after the function name
  3. And list the arguments comma separated
  4. End the parantehese AND add a colon:

def print_three(arg1, arg2, arg3):

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

How should your function name be?

A

It should only use characters AND underscore so for example

Print_two_again

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

How many spaces should the lines of code in a function be indented?

A

4 characters. No more, no less.

This usually happens automatically in your script-writing program

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

How do you end a function? (stop adding more to it)

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

What does it mean to run a function?

A

The same as to “use” or to “call” a function

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

What does it mean to call a function?

A

The same as to “run” or to “call” a function

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

What does it mean to call a function?

A

The same as to “use” or to “use” a function

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

What’s allowed for a function name?

A

Just like variable names, anything that doesn’t start with a number and is letters, numbers, and underscores will work

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

What does the * in *args do?

A

That tells Python to take all the arguments to the function and then put them in args as a list. It’s like argv that you’ve been using, but for functions. It’s not normally used too often unless specifi- cally needed.

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

Are functions and variables connected?

A

No. The functions you write in your script are not connected to the variables.

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

it will print:

We can just give the function numbers directly:

You have 20 cheeses!

You have 30 boxes of crackers!

Man that’s enough for a party!

Get a blanket.

As it will first “We can just give the function numbers directly:” and then all the content of the function (def) Cheese_and_crackers below it.

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

How should you write up an user-input-variable making sure that it will be an integer result?

A

Put raw_input inside int() for integer

For example

Eaten_packages = int(raw_input(“How many packages did you eat by now? “))

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

Is there a limit to the number of arguments a function can have?

A

It depends on the version of Python and the computer you’re on, but it is fairly large. The practical limit, though, is about five arguments before the function becomes annoying to use.

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

What does f.seek do?

A

The seek-function takes the “cursor” (or point of the mouse so to say) to a certain place in a file. For example, we take it to the very beginning with f.seek(0) so we are at the start of the file again

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

What does += do?

A

+= adds another value with the variable’s value and assigns the new value to the variable.

+= adds a number to a variable, changing the variable itself in the process (whereas + would not).

It adds the right operand to the left. x += 2 means x = x + 2

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

What does -= do?

A

+= subctracts a number from a variable, changing the variable itself in the process (whereas - would not).

It subtracts the right operand from the left. x -= 2 means x = x - 2

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

What does *= do?

A

*= multiplies a number with a variable, changing the variable itself in the process (whereas * would not).

It multiplies the right operand with the left. x *= 2 means x = 2x

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

What does /= do?

A

/= divides a variable with a number, changing the variable itself in the process (whereas / would not).

It divides the right operand with the left. x /= 2 means x = x/2

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

What does %= do?

A

%= finds modulus of a variable with a number, changing the variable itself in the process (whereas % would not). Modulus = the remainder when dividing.

It finds modulus so: x %= 3 means x = modulus of x/3

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

How does readline() know where each line is?

A

Inside readline() is code that scans each byte of the file until it finds a \n character, then stops reading the file to return what it found so far. The file f is responsible for maintaining the current position in the file after each readline() call, so that it will keep reading each line.

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

What does the return statement do/how does it work?

A

The RETURN statement writes out what it does to the user AND can also be used by other functions

The PRINT statement simply writes out what’s inside but cannot be used by other functions

Example explanation:

Def returnFunction(num):

Num = num *2

Return num

Def printFunction(num):

Num = num *2

Print(num)

If we write…

returnFunction(4)

we get an output of; 8

if we write….

printFunction(4)

we get an output of; 8

So as such they look the same.

But.. heres the difference.

X = returnFunction(4)

X #notice this one

8

Y = printFunction(4)

8

So the return function gives us “x” but the print-function doesn’t give us anything so we cannot use it later.

If we in python now ask for x we will get 8 but if we ask for y we won’t get anything.

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

How can I use raw_input() to enter my own values?

A

Remember int(raw_input())? The problem with that is then you can’t enter floating point, so also try using float(raw_input()) instead.

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

What does “return” mean?

A

It’s the same as “output”. Synonyms.

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

How do you write comments in python?

A

Write # and everything here after the hashtag will not be shown. It is called HASH or and OCTOTHORPE

But you can still write an octothorpe inside strings like:

Print “here we # like to go on Instagram”

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

How do you print some text that is followed by maths?

A

Remember to put a comma…

Print “Hens”, 25 + 5

Will return

Hens 30

Or

Print “What is 3 + 2?”, 3 +2

Will return

What is 3 + 2? 5

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

How do we create a variable in python?

A

Simple use =

For example

Cars = 100

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

What does %r do?

A

If you use %r, it will print no matter what, meaning that it will both print a character-variable (normally %s) and a number-variable (normally %d).

print “and with both character %r AND number %r” % (height, hair) # with %r we will end up printing both the character-variable AND the number-variable

and with both character 192 AND number ‘Dark-blond’

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

How can we repeat a printed string?

A

Just write asterisk and the number of times you want to repeat it. For example:

Print “.” * 20

Will return

………………..

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

How do you write a list of things but on different lines?

A

Use \n

For example:

Months = ‘Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug’

Print “Here are the months: “, months

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

How can you tab in some printed text?

A

Use \t

So for example

Print “””

I’ll do a list:

\t* Sugar

\t* Spice

\t* And everything nice

Will return

I’ll do a list

  • Sugar
  • Spice
  • And everything nice
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
167
Q

How can you ask users to input information/answer questions?

A

Use raw_input()

For example…

Print “How old are you?”,

Age = raw_input()

remember the comma after print

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

What do you need to do before you can use arguments?

A

Import argv by writing…

From sys import argv

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

Can you write strings inside raw_input()?

A

Yes. This is called a prompt. Help for the answer

For example

Print “What’s your birthday? “,

Raw_input(“DD-MM-YYYY” )

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

How do you define what your arguments are?

A

Write first the argument inoputs such as…

Script, filename = argv #script name and filename is defined here.

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

How can we create AND open a file at the same time?

A

note… you must define filename.

Open(filename, “w”)

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

How do you delete the contents of a file?

A

Target.truncate()

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

How do you write something into a file through your script?

A

Target.write()

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

How do you close a file?

A

Target.close()

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

How do you create a line break?

A

\n

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

How we get the number of bytes a file is long?

A

Len()

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

How do we open and read a file in the same line of code?

A

note you must define from_file

open(from_file, “r”)

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

How do you tell python that there is a function inside a certain file?

A

First you open command and run “python”

Then you “import file1” (whatever file it is)

And then tell python to run a certain function within “file1” by adding “dot” and the function’s name. For example:

E25.print_last_word(sorted_words)

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

How do you incorporate help statements in your python functions and later display them in command?

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

break_words(stuff)

A

This function will break up word for us.

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

print_first_and_last(sentence)

A

Prints the first and last words of the sentence.

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

print_first_and_last_sorted(sentence)

A

Sorts the words then prints the first and last one.

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

print_first_word(words)

A

Prints the first word after popping it off.

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

print_last_word(words)

A

Prints the last word after popping it off.

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

sort_sentence(sentence)

A

Takes in a full sentence and returns the sorted words.

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

sort_words(words)

A

Sorts the words.

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

What is a list?

A

lists

  • consist of a countable number of ordered values
  • fruits = [’orange ’ , ’apple ’ , ’pear ’ , ’banana ’ , ’kiwi’, ’apple’, ’banana’]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
188
Q

What is a tuple?

A
  • Immutable: Cannot be changed
  • The advantage is that you will know the position of the elements better and thus also make it easier for the computer to locate elements: provides for speed
  • You use it when you know that a variable will not change (e.g. days of the week)
  • Values are separated by commas
    • v = (’a’, ’b’, ’c’)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
189
Q

What is a set?

A

sets

  • unordered collection
  • NO duplicate elements
    • basket = {’apple’, ’orange’, ’apple’, ’pear’, ’orange’, ’banana’}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
190
Q

What is a dictionary?

A

dictionary

  • a set of keys: value pairs in which each key is unique
    • tel = {’jack’: 4098, ’john’: 4139}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
191
Q

What are classes and objects/instances?

A

classes are used to create new user-defined data structures that contain arbitrary information about something. Class is generic.

  • Class: Dog
    • species = ’mammal ’

an object/instance is a copy of the class with actual values (thus you have 1 class and multiple instances). Object/instance is specific.

  • Instance/object:
    • def __init__(self , name, age): self .name = name
    • self .age = age
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
192
Q

Will these two turn out the same results?

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

What is the difference between for- and while loops?

A
  • For-loops you know in advance the number of times it will execute. This is unknown with while-loops as it executes as long as the Boolean condition is True
  • Use a for-loop for definite iterations = if you know the maximum number of times that you need to execute the code
  • Use a while-loop if you require to repeat some computation until a condition is met that you cannot calculate in advance (needed for probabilistic results and human intervention)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
194
Q

what does a break statement do?

A

a break statement is used to immediately finish a loop.

(this is considered bad practice though)

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

what does a continue statement do?

A

a continue statement causes to skip the processing of the rest of the code for a loop BUT the loop continues with the subsequent iterations of the loop

(this is considered bad practice though)

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

What does “syntax error” refer to?

A
  • Missing punctuation characters, such as parentheses, quotation marks or commas
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
197
Q

What does “TypeError” refer to?

A
  • when you try to combine to incompatible items
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
198
Q

What does “NameError” refer to?

A
  • Normally when you have used a variable before assigning it a value
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
199
Q

What does “ValueError” refer to?

A
  • When the value passed to a function is not compatible with the function.
    • in the example as string inserted as if it was to be converted to an integer
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
200
Q

What is the standard format to ask for help in programming communities?

A

TIRTBV

  • Title
  • Introduce
  • Reproduce (others should be able to reproduce)
  • Tags
  • Background
  • Version
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
201
Q

How can you take the sqrt of numbers in python?

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

What is the difference between “a = 2” and “a == 2”?

A

A = 2 is defining a as 2

A == 2 is asking whether a is equal to 2 or not

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

What are 3 good rules for naming variables?

A
  • Names can not start with a number
  • Use _ when using space
  • Do not use capital letter (best practice is lowercase)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
204
Q

What are the 3 types of variables?

A
  • Float
  • Integer
  • Boolean
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
205
Q

How do you make addition, subtraction, multiplication, diversion, power functions and modulo?

A
  • Addition (i.e. +)
  • Substraction (i.e. -)
  • Multiplication (i.e. *)
  • Division (i.e. /)
  • Power functions (i.e. **)
  • Modulo (i.e. %)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
206
Q

What is a possible reason why (0.1+0.2-0.3) equals 5.551115123125783e-17 instead of 0?

A

The numbers have not been rounded and are only showing with one decimal.

To turn to 0 write: round(0.1+0.2-0.3)

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

How do you get the number of characters in a string?

A

Count number of characters in “Hello world”

my_text = ‘Hello world’

len(my_text)

11

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

What will….

my_text = ‘Hello world’

print(my_text[7])

return?

A

O

H = 0, e = 1, 2 = l, 3=l, o=4, “ “ = 5, w=6, o=7

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

If, my_text = ‘Hello world’, how can you write it out as both capital letters, small-case letters and as two different strings?

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

How can you reverse the order of a list?

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

If…

a = {‘a’,’b’,’c’,’d’}

b = {‘b’,’c’,’e’}

find all the elements in a and b result={‘a’, ‘b’, ‘c’, ‘d’, ‘e’}

A

a | b

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

If.. my_list = [1,2,3,4,2,2,2,2,5,5,5,5,9,9,9] how do we find the number of unique elements and how do we create set of these unique numbers?

A

len(set(my_list))

6

set(my_list)

{1, 2, 3, 4, 5, 9}

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

If…

a = {1, 2, 3}

b = {2, 3, 4}

how do you find the symmetric difference and what does it mean?

A

symmetric difference is everything that is not in the intersection, everything that’s not in common

print(a ^ b)

{1, 4}

214
Q

What will…

for i in range(1, 10):

if i%2 == 0:

print(i)

return?

A

this version is much more efficient as it will not spent time on

2

4

6

8

(A is the initial value, B is how far it should go and C is the steps)

(2, 10, 2) will turn out as 2, 4, 6, 8 (but not 10)

You can do this much more efficiently…

for i in range(2, 10, 2):

print(i)

215
Q

How can you print all multiplication tables from 1 to 10? (nested for-loops)

A

for x in range(1, 11, 1):

for y in range(1, 11, 1):

print((x*y), end=” “)

print()

216
Q

List comprehension:…. number = [1, 2, 3, 4], return their square value when > 8

A

list_Numbers = [1, 2, 3, 4]

l = [i**2 for i in list_Numbers if i**2 > 8]

l

with list comprehension you create a NEW list so you have [] around it

and then here I have defined the new list containing 9, 16 as “l”

you can add the new list to an existing list with .append

list_Numbers.append(l)

list_Numbers

[1, 2, 3, 4, [9, 16]]

217
Q

What do while-loops do?

A

Now let’s talk about WHILE-LOOPS, which executes an unknown number of times, as long as the Boolean condition is True

218
Q
A
219
Q

Write a loop that prints string characters one by one

A
220
Q

How can you…x^2: x in {0,1,2,…10}?

A
221
Q

Implement a x^4 in two nested comprehesion (x^2… then x^2)

A
222
Q

Create a function that prints a hello and a name of the person

A
223
Q

Write a program that outputs the first recurring character in a string.

myString = “ABCDEBC”

A
224
Q

What is the standard way to define a function in python (5 elements)?

A
225
Q

How can you adequately open a python file and read it?

A

you can also do it this way….

with open(‘data.txt’, ‘r’) as f

for line in f:

print(line)

this will read the file line by line

with open(‘data.txt’, ‘r’) as f:

data = f.read()

however, this reads everything in one line so if there are no line shifts in the file you will get one looooooong line

226
Q

How can you cope with consecutive errors in your program?

A

With try and except

try:

run my freaking code here and

except:

if it does not work then try to do this thing to fix it

227
Q

How you close a file?

A

write the specified name.close()

228
Q

How do you search for files that match a particular pattern, such as ‘.csv’?

A

You can use…

endswith()

startswith()

fnmatch.fnmatch()

229
Q

Retrieve all files and directories starting with ‘Py’

A
230
Q

What is the “finally” clause used for?

A
231
Q

What is NumPy?

A

NumPy is a Python module. It stands for ’Numerical Python’. It is a library consisting of multidimensional array objects and a collection of functions to process them.

232
Q

What is an array?

A
  • An array is a data structure consisting of a collection of values, each one identified by at least one positive index (can have more than one dimension; matrixes)
  • all values have the same type (int, float….)
  • (array = objects, ndarray = class name of these objects)
    • each element within this is a “dtype” (data-type) element in NumPy
233
Q

What are 5 great rules for writing clean code?

A
  • clear variable and function names
  • consistency in styling; ‘ vs “
  • Duplication vs. abstraction
  • Group like items in your code so that it is more reusable
  • Break long program into different files
234
Q

What’s the purpose of R and Python respectively?

A
  • R
    • Focuses on better, user friendly data analysis, statistics and graphical models
      • Academics, researchers, data scientists
  • Python
    • Object based and emphasizes productivity and code reliability
      • Programmers, data scientists
235
Q

How do you convert a list into ndarray?

A

x = [1, 2, 3]

a = np.asarray(x)

[1, 2, 3]

or…

a = np.asarray(x, dtype = float)

[1. , 2. , 3.]

236
Q

What are the parameters in array slicing?

[start:stop:step]

A
237
Q

How do you slice ndarrays with more than one dimension?

A
238
Q

How do you multiply two arrays element wise in NumPy?

A

np.multiply(a,b). Multiply two arrays element wise

239
Q

How do you perform a matrix multiplication in NumPy?

A

np.matmul(a,b). Performs a matrix multiplication

240
Q

How do you retrieve the largest element in NumPy?

A

np.amax(a). Retrieve the largest element

241
Q

What is a series in Panda?

A

A series is a one-dimensional labeled array and can be created using the following code

242
Q

What is the difference between syntax errors and exceptions?

A
  • Syntax errors occur when the parser detects an incorrect statement
  • An exception error occurs whenever syntactically correct python code results in an error
243
Q

How can you insert an exception?

A
244
Q

What are assertions and how can they be used?

A

Assertions are a systemic way to check that the inputs in a program are as expected by the program to avoid running into exception errors.

Often useful for:

  • checking parameter types, classes or values,
  • checking “can’t happen” situations such as dividing by 0
  • after calling a function to make sure that its return is reasonable

assert condition, error_message

245
Q

What is the “finally” clause for in python?

A

To implement some sort of action to clean up after executing a code.

246
Q

What is a DataFrame in Panda?

A
247
Q

How do you remove a column in Panda?

A

df = df.drop(columns = [‘ColumnName1’, ‘ColumnName2’])

248
Q

What does “applymap()” do in pandas?

A

The function applymap() applies a function to each element of a DataFrame (two-dimensional data structure)

apply() # by itself will only add to the specific column..

and

apply(______, axis = 1) # will add to a row

249
Q

What is “nan” in pandas?

A

nan = Not a Number

and is the result you get when applying functions to a DataFrame that has null values.

To detect missing values in your dataframe you can use the isnull() and notnull() functions.

250
Q

How do the different merge operations in Pandas look visually?

  • Natural join
  • full outer join
  • left outer join
  • right outer join
A
251
Q

What will….

for i in range(2, 10, 2):

print(i)

result in?

A
252
Q
A

you can also rename the function at the same time…

from fibo import fib_print as fibonacci

253
Q

How can you Retrieve all files and directories starting with ‘Py’?

A
254
Q

How can you Retrieve all .py files?

A
255
Q

How could study the efficiency of your code?

Use the “time()” function in python to check the time it takes for your code to run.

A
256
Q

How does a NumPy array of (3.7) look AND how can you make it?

NumPy takes rows FIRST and SECONDLY columns.

A
257
Q

What will happen if you try to create an array from the following python list… x = [(1, 2, 3), (4, 5)]?

A

it basically fails as the second list only contains two elements.

258
Q

Slicing multidimensional array (NumPy). How would you get [5, 6] from the following array?

A

a[-1][-2:]

returns… array ([5, 6])

why?

you basically have…

259
Q

How can you define an array with values from 0 to 100 and filter out odd numbers?

A

y = np.arange(101)

y[y%2==0]

array([0, 2, 4, 6, 8, 10, 12 ………. 100)]

note that arange only has one r.

260
Q

What will np.arange(3)+5 return?

A

this is broadcasting…

we have an array with 3 elements starting from 0 and we add 5.

array([5, 6, 7])

261
Q

What will np.ones((3, 3)) + np.arange(3) return?

A

this is broadcasting…

Here we first create np.ones((3,3)) which is

1, 1, 1

1, 1, 1

1, 1, 1

and then np.arange(3) on top of it so it is increasing by adding 0, then 1, then 2.

262
Q

What will np.arange(3).reshape((3, 1))+np.arange(3) return?

A

this is broadcasting…

np.arange(3) is..

0, 1, 2

BUT…. it is reshaped to 3 rows, 1 column… so…

0

1

2

and then we add another np.arange(3) to it…

0, 1, 2

and combine to…

0, 1, 2

1, 2, 3

2, 3, 4

263
Q

How can you edit an array and raise all numbers to the power of 2?

A

say…

a = np.array([1, 2, 3, 4])

for x in np.nditer(a):

print (x*x)

1, 4, 9, 16

note that you use “nditer”

264
Q

What are three classic functions used to manipulate ndarray elements?

A
  • # transpose; permutes the dimensions of an array
  • # concatenate; joins a sequence of arrays along an existing axis
  • # append; appends the values to the end of an array
265
Q

What will happen if you use np.transpose(a) on “a = np.array([[1, 2], [3, 3]])

A

it starts as..

array([[1, 2],

[3, 4]])

and transpose will change the dimensions of the array (flip rows and columns) so after np.transpose(a) we have…

array([[1, 3],

[2, 4]])

266
Q

How do you join two different arrays by rows and by columns respectively?

say you have..

a = np.array([[2, 2], [3, 3]])

b = np.array([[4, 4], [6, 6]])

A

by rows you would….

np.append(a, b, axis = 0) #axis 0 for rows

result…

array([[2, 2],

[3, 3],

[4, 4],

[6, 6]])

and for columns…

np.append(a, b, axis = 1) #axis 1 for columns.

result….

array([[2, 2, 4, 4],

[3, 3, 6, 6]])

267
Q

How can you create an array of ALL zeros?

A

a = np.zeros((3, 3))

print(a)

[[0, 0, 0]

[0, 0, 0]

[0, 0, 0]]

268
Q

How can you create an array that has “42” in each element?

A

x = np.full((3, 3), 42)

print(x)

[[42, 42, 42]

[42, 42, 42]

[42, 42, 42]]

269
Q

How can you create an array with random values?

A

e = np.random.random((2, 2))

270
Q

if we have x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]), what will… x[-3: 3: -1] return?

A

first we find -3, which is number 7

then we go from -3 until 3 which happens to be number 3 as it is in the 4th location in the row (0, 1, 2, 3)

and lastly we return the values in OPPOSITE order because of -1

array([7, 6, 5, 4])

271
Q

What is the key difference to remember between NumPy and Pandas?

A
  • NumPy = First, ROW, Second, COLUMN
  • Pandas = First, COLUMN, Second ROW
272
Q

How can you select multiple rows in Pandas?

A

Multiple rows can be selected using ’:’ operator as in numpy arrays

df[1:3]

just remember that in Pandas, you pick COLUMN FIRST, and row second

273
Q

How can you get information about a file in pandas?

A

if the file name is titanic.csv

titanic.info()

will return

274
Q

How can you get descriptive statistics?

A

if the file name is titanic.csv

titanic.describe()

275
Q

What are the components in making a histogram?

A
  • titanic.hist(
    • bins = # the width of bars (lower is wider)
    • figsize=(16, 9) #is the proportions of the figures
    • grid=False #whether you want a grid or not
  • plt.show() #and then show the histogram
276
Q

How do you make a histogram of the data within a specific column?

A

titanic. Age.hist(bins = 50, figsize=(12, 6), grid=True)
plt. xlabel(‘Age’)
plt. ylabel(‘Number of people’)
plt. show()

277
Q

Is NaN == NaN?

A

No…

NaN == NaN is False

NaN is special in that it doesn’t have a real value, so comparing it to itself doesn’t return true. Essentially, NaN is equal to nothing, not even NaN

Because of this, the only safe way to tell whether or not a value is missing in a DataFrame is by using the isnull() function. Indeed, this function can be used to filter rows with missing values;

edu[edu[“Value”].isnull()]

278
Q

How can be said about columns and rows in pandas?

A
  • COLUMNS
    • are Series, which consists of several values, where each value has an index
  • ROWS
    • have a specific index to access them and be any name or value
279
Q

If we just define a series as, c = pd.Series([1956, 1967, 1989, 2000]), what will the index be?

A

when not specifying the index, they will simply be a sequence of integers starting from 0.

0, 1, 2, 3 in this case

280
Q

How can we specify the index for the following series, c = pd.Series([1956, 1967, 1989, 2000])?

A
281
Q

What are the ‘head’ and ‘tail’ methos for?

A

Helps show the first (by default = 5) rows in a dataset so we can see how the data looks.

If you insert a number in head(), you will get that number of rows instead of the default 5 rows.

The tail() method is the opposite in the way that it gives you the LAST 5 rows.

282
Q

How can you get the names of the columns and rows in a DataFrame?

A

filename.columns

and

filename.index

283
Q

What is the difference between edu[10:14] and edu.loc[10:14]?

A

edu[10:14] will provide the rows from 10th to 13th position BUT this does not take your index labels into account. Only the position.

edu.loc[10:14] will return the values of all the rows between the row labeled 10 and the row labede 14. This could be 10,11,12,13,14 but it could just as well be only one value OR it could be many values. It could also be a row which is indexed 100 if that row has been placed between the rows labeled 10:14.

284
Q

what is the difference between writing edu.max(axis=0) and edu.max(axis=1)?

A

axis = 0 finds the maximum value for each column (finding max within the rows for each column)

while

axis = 1 find the maximum value for the row (finding max within the column for each row)

285
Q

What is the difference between pandas and pythons interpretation of NaN?

A

In Python ,NaN values propagate through all operations without raising an exception. In contrast, Pandas operations exclude NaN values representing missing data. For example, the pandas max function excludes NaN values, thus they are interpreted as missing values, while the standard Python max function will take the mathematical interpretation of NaN and return it as the maximum:

286
Q

What is a lambda function?

A

It is essentially just a function without a name.

It is written in-line.

For example…

287
Q

What is the following doing?

edu[“ValueNorm”] = edu[‘Value’]/edu[‘Value’].max()

edu.tail()

A

we are defining a new column called ‘ValueNorm’ by taking ‘Value’/the highest number in the ‘Value’ column

lastly we ask to see the 5 last rows of the updated DataFrame using tail()

288
Q

How can you insert a new row at the bottom of a DataFrame?

A

using the append function, which receives as argument the new row represented as a dictionary where the keys are the name of the existing columns and the values the associated values.

It is important to the the ignore_index flag to True, otherwise the index 0 is given to this new row which will produce an error if it already exists (may potentially overwrite if not set to True)

289
Q

How can you easily delete the last row in a Dataframe?

A

simply use the drop and max functions. Why max? because then we will be taking the last row if the index is ordered at least.

290
Q

How can we fill in NaN values?

A

using fillna()

eduFilled = edu.fillna(value={‘Value’:0})

will fill in the NaN with 0 instead but be careful about doing so will change the statistics of your data when using pandas.

291
Q

What will the following give us?

edu. sort_values(by=’Value’, ascending= False,inplace=True)
edu. head()

A

here we are using the sort function to sort the data

by

‘Value’ and NOT in ascending order (then it must be descending order), meaning that we will get the 5 highest values as we use edu.head() which by default give 5 rows.

292
Q

What will the following give us?

A

here we are using groupby to group some data.

we are clearly interested in GEO (countries) and the values.

We are grouping by Geo (country) and asking for the mean of values for each country.

next, we ask pandas to sort this data by values in a descending order.

293
Q

What is the following doing?

A
  1. first we are filtering the data with a boolean expression asking for data dated later than 2005 from the TIME column
  2. secondly, we use the pd.pivot_table function to rearrange our data such that the index/rows become GEO/countries and columns will be equal to time (the time values newer than 2005 as we are using the filtered data)
  3. lastly we ask to see the first 10 rows
294
Q

Say we have x = 9, y = 3,….

then we write x + 10

and python returns 19

How do we get the previous result + y?

A

obviously we could just write 19 + y and get 22

we could also press the arrow key up in command and pick up x + 10 and add + y to get 22

the most correct and simplest way though is:

_ + y #underscore + y

22

295
Q

say

name = ‘Youtube’

name

name[1:4]

A

‘out’

296
Q

say you have a list of numbers

numbers = [33, 44, 55, 66, 77, 88]

A
297
Q

what are the two ways that we can remove 44 from the list?

[33, 44, 55, 66, 77, 79, 88, 79]

A
298
Q

how can you add multiple values to an existing list?

A
299
Q

How do you define a list, a set and how do you define a tuple?

A
  • list =[]
    • most flexible and can be both numbers and strings
  • set = {}
    • like list BUT… will not maintain sequence and does not support duplicate values
  • tuple = ()
    • tuples you cannot change the values the same way as lists. Thus, use only tuples for things that are fixed.
      • tuples are though useful because they are faster for python to run
300
Q

What are the different data types in Python?

A
  • None
  • Numeric
    • Int, float, complex, bool
  • List []
  • Tuple ()
  • set {}
  • string “”
  • range (2,10,2)
  • Dictionary
301
Q

If x = 2, what will be the result of x *= 3?

A

2 * 3 = 6

302
Q

If x = 2, what will be the result of x **= 3?

A

2^3 = 8

303
Q

Why is bin(25) = ‘11001’ for a computer?

A

computers think binary..

11001

1*(2^4), 1*(2^3), 0*(2^2), 0*(2^1), 1*(2^0) = 25

304
Q

How do you swap the values of the

a = 5

b = 6

A
305
Q

How do you swap the values of the

two variables below in the easiest way?

c = 10

d = 15

A
306
Q

What are python’s bitwise operators?

A
  • complement (∼) (‘tilde’ symbol)
  • and (&)
  • Or (І)
  • XOR (^)
  • Left shift (<<)
  • right shift (>>)
307
Q

Why is 10 << 2 returning 40?

A

because.,..,

10 is in binary…. 1010

meaning that it is essentially

1010.000000000 (infinite number of 0s after the decimal)

now we shift 2 to the left so we get

101000.00000

and thus 101000

1*32, 0*16, 1*8, 0*4, 0*2, 0*1 = 40

308
Q

why is 100 >> 3 returning 12?

A

because…

100 is in binary 1100100

meaning that it is essentially

1100100.000000(all the 0 in the world if needed)

now we shift 3 to the right so it is now..

1100.100 and thus

1*8, 1*4, 0*2, 0*1 = 12

309
Q

what are math.floor() and math.ceil() for?

A

using math.floor and math.ceil (after importing math, import math to python) is essentially round numbers to the nearest integer floor or ceiling.

for 2.7, the floor is 2 and ceiling is 3

for 100.14 the floor is 100 and ceiling is 101

310
Q

how can you print ejendomsinvestoren 5 times?

A
311
Q

How can you print each even number from 0 to 100?

A
312
Q

how can you print all numbers from 1 to 100

but excluding those that are divisible by 3 and 5?

A
313
Q

What is the difference between ‘continue’ and ‘break’?

A

if i in range(5):

if i == 3:

break

print(“Hello “, i)

with break it will stop printing so we only get; Hello 0, Hello 1, Hello 2

if i in range(5):

if i == 3:

continue

print(“Hello “, i)

with break it will CONTINUE printing so we only get; Hello 0, Hello 1, Hello 2, Hello 4,

314
Q

How would you create the following:

####

####

####

####

?

A
315
Q

How would you create the following:

#

##

###

####

?

A
316
Q

How would you create the following:

####

###

##

#

?

A
317
Q

What does it mean to use FOR ELSE?

A

To make a for-loop in which you have an if-elif-else statement WHERE the “else” IS NOT indented.

318
Q

say we have a list of numbers

nums = [12, 15, 18, 23, 26]

A
319
Q

What is TypeCode used for?

A

You use TypeCode to specify which type the values are of for an ARRAY

import array

array.array(‘b’, [5, 7, 8, 10])

320
Q

how can you ask a user to enter values to an array?

A
321
Q

Why do we need numPy?

A

For multi-dimensional arrays

322
Q

What are the six ways to create arrays with NumPy?

A
  1. array()
    1. arr = array([1, 2, 3, 4, 5])
  2. linspace()
    1. arr = linspace(0, 16, 32)
      1. start, stop and step/parts (BUT.. step is splitting up into parts here)
        1. from 0 to 16 and split into 32 numbers.
      2. if not specifying the number of parts it is 50 by default
  3. logspace()
    1. arr = logspace(1, 40, 5)
      1. start, stop and parts/step
        1. split up to 5 numbers between 1 and 40 according to log
  4. arrange()
    1. arr = arange(1, 15, 2)
      1. will return… 1, 3, 5, 7, 9, 11, 13
      2. start, stop and STEP
  5. zeros()
    1. arr = zeros()
      1. array of ALL 0s
      2. fx arr = zeros(5) will return [0, 0, 0, 0, 0]
  6. ones()
    1. arr = ones()
      1. array of ALL 1s
323
Q

copying an array in python SAME ID

A
324
Q

how can you add two arrays?

A
325
Q

how can you make two arrays into one long array?

A
326
Q

copying an array in python DIFFERENT ID

A
327
Q

How can you check how many dimensions an array contain?

A

print(arr1.ndim)

328
Q

How can you check the shape of an array?

A

print(arr.nshape)

329
Q

How can you check the size of an array?

A

print(arr.size)

330
Q

how can you convert a 2d-array into 1d-array?

multi-dimensional arrays…

A
331
Q

how can you convert a 2d-array into 3d-array?

multi-dimensional arrays…

A
332
Q

define a simple function and call it “greet”

A
333
Q

define a function that can add two numbers

A
334
Q

Create a function that can sum an unknown amount of numbers

A
335
Q

What is the difference between a local and global variable?

A
336
Q

How can you pass a LIST to a function in Python and count #odd and #even?

A
337
Q

How can you create a Fibonacci Sequence?

A
338
Q

How can you make a function that find the Factorial of a given number?

A
339
Q

What does recursion mean?

A
340
Q

How can we make factorial using recursion?

A
341
Q

What is a lambda function?

A

essentially an anonymous function = a function with no name

342
Q

filter map reduce

how can you sort this list for even numbers using lambda?

nums = [3, 2, 6, 4, 3, 9, 11]

A
343
Q

what is __name__ and why is it useful?

A

it is a special variable in python used to avoid running an entire

344
Q

what is the better word to use for “function”?

A

METHOD

345
Q

How do “class” and “methods” work together?

A

Class are overall..

class Computer:

and then the methods/functions are part of that class and must be called with…

computer.config(com1)

just as we write numpy.something

but.. you can actually also write… com1.config() which is more likely what you will see (calling the method FROM the object)

346
Q

What is object oriented programming?

A

thinking in objects just like everything we need to do in real life

we need “objects” for such as a laptop, a camera, a frying pan etc.

in programming we need to build objects also and put them together

what matters is DESIGNING objects, not manufacturing them as that can

be done anywhere, and in the computer world they can be replicated

billions of times quickly.

347
Q

What are the two types of variables?

A
348
Q

What is inheritance in python?

A

Class A:

Class B(A):

Class C(B):

but we can also say that C is inheriting from A and B (mom and dad) who are unrelated by…

Class A:

Class B:

Class C(A, B)

349
Q

What does “polymorphism” mean and how can it be implemented?

A

polymorphism = poly(many) morphism(forms) = objects can take many forms. It can behave in multiple ways.

duck typing

operator overloading

method overloading

method overriding

350
Q

What is # method overloading?

A

method overloading: if you have two methods with the same name,

def average(a,b) and def average(a, b, c) it is method overloading

one takes two parameters and one takes three

351
Q

What is # method overriding?

A

two methods with the same name AND the same number of parameters

also, if class B(A): inherits what is in A, but B has something itself, it will override the information from A.

Say class A: contains the car of your dad

Say class B(A): contains nothing, then your father’s car is yours(inheritance) but if you have your own car within B, it will be that one.

352
Q

What is a logical error?

A

when your code returns a value but the value is wrong.

if 3+2 returns 4, you have made a logical error

353
Q

What is a runtime error?

A

when the code does not work during part of the run time…

this is for example if people put in a string and you only allow for integers, float when only integers allowed, if a negative value or if divided by 0 and so forth.

354
Q

How do you handle exceptions?

A

use the

try:

except: #can use multiple excepts

except:

except:

finally:

setup….

355
Q

Why is multi-threading important?

A
356
Q

Is python a compiled or interpreted language?

A

It is actually both…

  • Compiled language
  • the computer does not understand the language so we compile the code we write for the computer to understand in binary form (0101010101010110110)
  • Interpreted language
  • also interpreted as…
  • source code => compiled => byte code => interpreted by python virtual machine => machine learning
  • it is taking things line by line
357
Q

How do you swap the values; a = 8 and b = 10?

A

a = 8

b = 10

then…

a = c # now c is 8

b = a #now a is 10

c = b #now b is 8

a = 10

b = 8

358
Q

Say you have the list…

areas = [“hallway”, 11.25, “kitchen”, 18.0, “living room”, 20.0, “bedroom”, 10.75, “bathroom”, 9.50]

find the # Sum of kitchen and bedroom area and call it: eat_sleep_area

A
359
Q

If.. areas = [“hallway”, 11.25, “kitchen”, 18.0, “living room”, 20.0, “bedroom”, 10.75, “bathroom”, 9.50]….. update the area of the bathroom area to be 10.50 square meters instead of 9.50.

AND Make the areas list more trendy! Change “living room” to “chill zone”

A
360
Q

If.. areas = [“hallway”, 11.25, “kitchen”, 18.0, “living room”, 20.0, “bedroom”, 10.75, “bathroom”, 9.50]….. now You decide to build a poolhouse and a garage. Add this to the list..

A
361
Q

Say you have… areas = [“hallway”, 11.25, “kitchen”, 18.0, “chill zone”, 20.0, “bedroom”, 10.75, “bathroom”, 10.50, “poolhouse”, 24.5, “garage”, 15.45]… how do we remove the poolhouse?

A

del(areas[-4:-2])

362
Q

What is wrong here?

A

The problem we have is that area_copy = areas is not explicit enough in copying the areas list. Thus, when we change the values of areas_copy[] to 5 we are in fact also changing the value [0] in the original areas list.

To avoid changing the original list when making changes in the copy, we must use either list(areas) or areas[:]

363
Q

Say you have.

first = [11.25, 18.0, 20.0]

second = [10.75, 9.50]

now merge the two lists and write them in descending order…

A
364
Q

How do you check the components of an in-built python function?

A

you can always search the internet but otherwise use..

help(function_name) so for example help(max), help(sorted) and so forth

365
Q

What is important to remember about methods?

A

… METHODS call functions on objects

there are different methods available to different types of objects.

also, sometimes the same method is available to different types of objects but behave differently,.

fam.index()

366
Q

Call the method “upper” on place = “poolhouse

A

place_up = place.upper()

print(place)

print(place_up)

poolhouse

POOLHOUSE

367
Q

you have.. areas = [11.25, 18.0, 20.0, 10.75, 9.50], how can you add 24.5 and 15.45 to the list?

A

areas. append(24.5)
areas. append(15.45)

368
Q

What is two of the reasons the NumPy ARRAY is better than LIST?

A
  • the ARRAY is able to perform calculations on the entire set of values
  • You can also use boolean operators to obtain results from arrays.
369
Q

What are some of the disadvantages of ARRAY compared to LIST?

A

Array cannot contain different value types. If having different value types, type coercion will take place

370
Q

In NumPy, # Print out the 50th row of np_baseball

A

print(np_baseball[49])

371
Q

In NumPy, # Select the entire second column of np_baseball and call it np_weight_lb

A

np_weight_lb = np_baseball[:, 1]

372
Q

In Numpy, Create numpy array np_height_in that is equal to first column of np_baseball.

  • Print out the mean of np_height_in.
  • Print out the median of np_height_in.
A

NOTE that the format is…. np.builtinfunction(nameofarray)

and… rememebr that NumPy has ROW first, column second so [:, 0]

373
Q

In Numpy, # Print out correlation between first and second column. Replace ‘None’

corr = None

print(“Correlation: “ + str(corr))

A
374
Q

Say you have two np.rrays…

np_heights = np.array(heights)

np_positions = np.array(positions)

Now define the heights of goalkeepers (GK), gk_heights and the height of other players, other_heights

A

Heights of the goalkeepers: gk_heights

gk_heights = np_heights[np_positions == ‘GK’]

Heights of the other players: other_heights

other_heights = np_heights[np_positions != ‘GK’]

375
Q

Say you have two np.arays…

Heights of the goalkeepers: gk_heights

gk_heights = np_heights[np_positions == ‘GK’]

Heights of the other players: other_heights

other_heights = np_heights[np_positions != ‘GK’]

How can we get the median for each of them and print it?

A
376
Q

How should you import matplotlib.pyplot?

A

import matplotlib.pyplot as plt

377
Q

How do you make a line plot and a scatter plot in matplotlib.pyplot?

A

plt. plot(x, y) = line plot
plt. scatter(x, y) = scatter plot (no lines, just data points)

378
Q

How do you plot something on a logarithmic scale?

say you have plt.scatter(gdp_cap, life_exp)

A

plt. xscale(‘log’) #changing the x-axis to logarithmic
plt. show()

379
Q

What are the most basic arguments in creating a histogram?

A

import matplotlib.pyplot as plt

plt.hist(dataname, bins = 10) #it is 10 by default

380
Q

How do you add axis names to your pyplots?

A

plt. xlabel(‘price’)
plt. ylabel(‘quantity’)

381
Q

How do you add a title to your pyplots?

A

plt.title(‘my title here’)

382
Q

How do you change the increments on x and y axis?

A

for example..

plt.yticks([0, 2, 4, 6, 8, 10])

383
Q

Say your x-axis is logarithmic and go from 1,000 to 100,000, how can we simplify the x-axis ticks?

A

tick_val = [1000, 10000, 100000]

tick_lab = [‘1k’, ‘10k’, ‘100k’]

plt. xticks(tick_val, tick_lab)
plt. show()

384
Q

Explain this…

plt.scatter(x = gdp_cap, y = life_exp, s = np.array(pop) * 2, c = col, alpha = 0.8)

A
  • We are making a scatter plot
  • with gdp_cap on the x-axis
  • and life_exp on the y-axis
  • size is set to np.array(pop) * 2 #doubling the sizes of the bubbles
  • c = col => colors are set acording to what has been defined in col which in this case is different colors depending on continent of origin
  • alpha = 0.8 sets the opacity/transparency of the bubbles. 0 = 100 % opacity
385
Q

How can we mark China and India on the plot?

A

We can add text with

plt. text(1550, 71, ‘India’)
plt. text(5700, 80, ‘China’)

we obviously need to know these values first.

386
Q

Dictionary… check which keys are in europe

A

print(europe.keys())

dict_keys([‘spain’, ‘france’, ‘norway’, ‘germany’])

387
Q

Dictionary… called “europe”, print the value that belongs to the key, ‘norway’

A

print(europe[‘norway’])

oslo

388
Q

When would you use list vs. dictionary?

A

List

  • If you want to easily access values in an ordered matter (using indexes)

Dictionary

  • When you want more freedom and have a unique table in which the unique keys can take different forms
389
Q

dictionary… say we have..

europe = {‘spain’:’madrid’, ‘france’:’paris’, ‘germany’:’berlin’, ‘norway’:’oslo’ }

Now add Italy to it.

and check if it has been added…

A

europe[‘italy’] = ‘rome’

now ask..

europe(‘italy’ in europe)

True

390
Q

Somebody thought it would be funny to mess with your accurately generated dictionary. An adapted version of the europe dictionary is available in the script on the right.

europe = {‘spain’:’madrid’, ‘france’:’paris’, ‘germany’:’bonn’,

‘norway’:’oslo’, ‘italy’:’rome’, ‘poland’:’warsaw’,

‘australia’:’vienna’ }

Can you clean up?

A
391
Q

Dictionaries:

europe = { ‘spain’: { ‘capital’:’madrid’, ‘population’:46.77 },

‘france’: { ‘capital’:’paris’, ‘population’:66.03 },

‘germany’: { ‘capital’:’berlin’, ‘population’:80.62 },

‘norway’: { ‘capital’:’oslo’, ‘population’:5.084 } }

print out the capital of France:

A

print(europe[‘france’][‘capital’])

392
Q

Dictionaries:

europe = { ‘spain’: { ‘capital’:’madrid’, ‘population’:46.77 },

‘france’: { ‘capital’:’paris’, ‘population’:66.03 },

‘germany’: { ‘capital’:’berlin’, ‘population’:80.62 },

‘norway’: { ‘capital’:’oslo’, ‘population’:5.084 } }

print out the capital of France:

  • Create a dictionary, named data, with the keys ‘capital’ and ‘population’. Set them to ‘rome’ and 59.83, respectively.
  • Add a new key-value pair to europe; the key is ‘italy’ and the value is data, the dictionary you just built.
A
393
Q

Pandas: ‘Say you have a DataFrame ‘cars’, how can we change the index labels?

A

row_labels = [‘US’, ‘AUS’, ‘JPN’, ‘IN’, ‘RU’, ‘MOR’, ‘EG’]

cars.index = row_labels

394
Q

Pandas, say you have a file, cars.csv, how can you import it as a DataFrame?

A

cars = pd.read.csv(‘cars.csv’) #remember ‘’ around the file name

print(cars)

395
Q

Pandas: What is the difference between the ouput of:

cars[‘cars_per_cap’]

and

cars[[‘cars_per_cap’]]

A

The single bracket version gives a Pandas SERIES while the double bracket version gives a Pandas DataFrame.

396
Q

Pandas.. What is the difference between loc and iloc?

A

loc = label-based meaning that you have to specify rows and columns based on their row and column labels.

iloc = integer index based so you have to specify rows and columns by their integer index starting from 0.

397
Q

Pandas.. What does this do….. cars.iloc[[3, 4], 0]

A

We ask for the rows indexed 3 and 4, and then the column indexed 0.

we get it as a DataFrame because of [[]]

398
Q

Pandas.. Print out the drives_right value of the row corresponding to Morocco (its row label is MOR

A
399
Q

Pandas..Print out a sub-DataFrame, containing the observations for Russia and Morocco and the columns country and drives_right.

A

Print sub-DataFrame

print(cars.loc[[‘RU’, ‘MOR’], [‘country’, ‘drives_right’]])

400
Q

Pandas.. from ‘cars’, # Print out drives_right column as DataFrame

A

print(cars.loc[:,[‘drives_right’]])

can also be done with iloc if we know the index number of the drives_right column

401
Q

What will “alpha” <= “beta” return and why?

A

True

Because Python determines the relationship based on alphabetical order when working with strings.

402
Q

How can you use AND, OR, NOT operators in NumPy?

A

Arrays work slightly differently so you need to use the AND, OR, NOT equivalent functions in NumPy, which are:

  • np.logical_and()
  • np.logical_or()
  • np.logical_not()

Example: np.logical_and(my_house < 11, your_house < 15)

403
Q

If we have…

areas = [11.25, 18.0, 20.0, 10.75, 9.50]

How can we print the values one by one AND the index values?

A

use for loop + enumerate

404
Q

If we have…

house = [[“hallway”, 11.25],

[“kitchen”, 18.0],

[“living room”, 20.0],

[“bedroom”, 10.75],

[“bathroom”, 9.50]]

Write a for loop that goes through each sublist of house and prints out the x is y sqm, where x is the name of the room and y is the area of the room.

A
405
Q

How can you iterate over key values in a dictionary?

A

Key values in dictionary

  • for key, val in my_dict.items():
    • # use .items
406
Q

how can you iterate over all elements in a numpy array?

A

elements in an array

  • for val in np.nditer(my_array):
    • # use np.nditer
407
Q

how can you iterate over all elements in a pandas DataFrame?

A

for pandas Dataframes

  • for lab, row in brics.iterrows():
    • # use .iterrows()
408
Q

How can you iterate over key values in a dictionary and how can you iterate over all elements in a numpy array? and for Pandas DataFrames?

A

Key values in dictionary

  • for key, val in my_dict.items():
    • # use .items

elements in an array

  • for val in np.nditer(my_array):
    • # use np.nditer

for pandas Dataframes

  • for lab, row in brics.iterrows():
    • # use .iterrows()
409
Q

If we have…

europe = {‘spain’:’madrid’, ‘france’:’paris’, ‘germany’:’berlin’,

‘norway’:’oslo’, ‘italy’:’rome’, ‘poland’:’warsaw’, ‘austria’:’vienna’ }

Write a for loop that goes through each key:value pair of europe. On each iteration, “the capital of x is y” should be printed out, where x is the key and y is the value of the pair.

A
410
Q

We have…

import pandas as pd

cars = pd.read_csv(‘cars.csv’, index_col = 0)

and need to make the he first iteration print out “US: 809”, the second iteration “AUS: 731”, and so on.

“country: cars_per_cap”

A

(already set as “lab” and “row”)

411
Q

In a DataFrame, how can you create a new column by calling a function on another column?

A

use .apply

very important feature.

.apply

412
Q

random; How can you roll a dice?

A
413
Q

Why is it generally better to use “return” rather than “print” for functions?

A

Returning values is generally more desirable than printing them out because, as you saw earlier, a print() call assigned to a variable has type NoneType.

414
Q

How can you return multiple values from a function?

A

Using tuples!!

415
Q

Explain this code:

A
416
Q

What are the different function scopes?

A
  • GLOBAL scope
    • defined in the main body of a script
  • LOCAL scope
    • defined inside a function
  • BUILT-IN scope
    • names in the pre-defined built-ins module (such as ‘print’)
      • NOTE; you need tim ‘import builtins’ to use this.
        • writing ‘dir(builtins)’, you can see all builtin functions

Python ALWAYS search for a value in LOCAL scope first, second GLOBAL scope and lastly BUILT-in scope.

417
Q

How do you set a default parameter?

A

Here echo=1 => echo is set to 1 unless otherwise specified.

Thus, in line 13, we do not specify echo but it will return 1 time, while in line 15, we ask it to return 5 times

418
Q

How do you create a variable-length argument?

A

Use (*args) #single *

It will turn a tuple of values.

def_sums(*args):

419
Q

How do you create a variable-length keyword argument?

A

Use (**kwargs) #double **

It will turn a dictionary of values

def report_status(**kwargs):

420
Q

How would you write a lambda function add_bangsthat adds three exclamation points ‘!!!’ to the end of a string a? and How would you call add_bangs with the argument ‘hello’?

A
421
Q

Convert this function to a lambda function

A
422
Q

what is happening here?

A
423
Q

If…

fellowship = [‘frodo’, ‘samwise’, ‘merry’, ‘pippin’, ‘aragorn’, ‘boromir’, ‘legolas’, ‘gimli’, ‘gandalf’]

Using lambda, create a new list that contains only strings that have more than 6 characters…

A
424
Q

What are the ways to write in error messages?

A

try:

return:

except:

and…

if ‘something’:

raise _____Error(‘explanation’)

try:

return

except: _____Error

425
Q

How can you select and print the retweets ‘RT’ from a Twitter DataFrame;

A
426
Q

What’s the difference between iterators and iterables?

A

Iterable

  • An object with an associated iter() method
  • Examples: lists, strings, dictionaries, file connections
  • Applying iter() to an iterable creates an iterator

Iterator

  • Produces next value with next()
427
Q

How can you iterate at ONCE?

A

use..

print(*it)

428
Q

What does the “enumerate()” function do?

A

It returns an enumerate object that produces a sequence of tuples, and each of the tuples is an index-value pair.

429
Q

In this exercise, you are given a list of strings mutants and you will practice using enumerate() on it by printing out a list of tuples and unpacking the tuples using a forloop.

Now, create a list and unpack the tupls using a for loop:

A
430
Q

What does zip() do?

A

It takes any number of iterables and returns a zip object that is an iterator of tuples.

If you want to print the values of a zip object, you need to convert it to a list and then print it.

431
Q

Say we have mutants and powers, how do you create a zip object? AND then unpack it again?

A
432
Q

Why may one want to load data in chunks and how is it done?’

A

Because there can be too much data to hold in memory.

pandas function: read_csv()

  • specify the chunk with… chunksize

read_csv(‘filename’, chunksize=xxxx)

433
Q

How can you iterate over the file tweets.csv in small portions and adding new words to a dictionary and counting the number of times each word is in that dictionary?

A
434
Q

Say we have…

doctor = [‘house’, ‘cuddy’, ‘chase’, ‘thirteen’, ‘wilson’]

How would a list comprehension that produces a list of the first character of each string in doctor look like?

A
435
Q

How can you add a condition to a list comprehension?

A
436
Q

What are generators?

A

uses ()

Great for very big numbers as it does not store everything in memory (as a list comprehension would do).

While a list comprehension produces a list as output, a generator produces a generator object.

437
Q

Create a generator object that will produce values from 0 to 30. Assign the result to result and print it using a for loop

A
438
Q

What is the basic format of a list comprehension?

A
439
Q

The lists2dict() function has already been preloaded, together with a couple of lists, feature_names and row_lists. feature_names contains the header names of the World Bank dataset and row_lists is a list of lists, where each sublist is a list of actual values of a row from the dataset.

A
440
Q

Write a list comprehension to generate a list of values from pops_list for the new column ‘Total Urban Population’. The output expression should be the product of the first and second element in each tuple in pops_list. Because the 2nd element is a percentage, you also need to either multiply the result by 0.01or divide it by 100. In addition, note that the column ‘Total Urban Population’ should only be able to take on integer values. To ensure this, make sure you cast the output expression to an integer with int().

A
441
Q

How do you read a file? (best practice)

A
442
Q

What will “! ls” do?

A

! ls will display the contents of your current directory.

443
Q

How do you open a file using the “with”-method?

A
444
Q

What is flat files?

A
  • Text files containing records
  • Table data
  • Record: row of fields or attributes
  • Column: feature or attribute

Flat files often have headers

.csv, .txt (tab-delimited),

445
Q

What are the main three ways to import data to NumPy?

A
  • np.loadtxt(filename, delimiter=‘,’)
    • digits = np.loadtxt(file, delimiter=’, skiprows = 1’)
    • ‘,’ for comma-separated
    • ‘\t’ for tab-delimited
    • skiprows = 1 will skip the first row if we have headers for example
    • usecols => lets you specify which rows wish to keep
  • np.genfromtxt(‘filename’, delimiter = ‘,’, names=True, dtype=None)
    • To be used when you have different data-types (strings, float, etc).
    • names = True => means there is a header
  • np.recfromcsv()
    • Behaves similarly to np.genfromtxt BUT has default dtype=None, default ‘,’ as delimiter and default names=True.
446
Q

What is pandas providing to data scientists that Numpy does not?

A
  • Two-dimensional labeled data structures
  • Columns of potentially different types
  • Manipulate, slice, reshape, groupby, join, merge
  • Perform statistics
  • Work with time series data

Pandas is for data analysis and modeling.

447
Q

From the file, digits.csv, import the first 5 rows to a DataFrame, build a numpy array from this DataFrame and print the datatype

A
448
Q

How is delimiter defined in pandas import?

A

pd.read_csv(filename, sep = ‘\t’, comment = ‘#’, na_values = ‘Nothing’)

449
Q

What are pickled files?

A
  • A file type native to python
  • Not readable by humans but readable by python/machines
    • JSON files are an option if you want it readable by humans
  • Motivation; many datatypes for which it isn’t obvious how to store (such as lists, dictionaries etc.)
  • ‘rb’ means read only binary
450
Q

How do you import Excel files to pandas?

A

figureo ut what the sheets in excel are called by

print(data.sheet_names) here as the file has been set to ‘data’

and to make dataframes of those sheets, use .parse and the sheet name OR sheet index

451
Q

Import the file, battledeath.xlsx and display the sheet names

A
452
Q

What is happening here?

A
453
Q

What are SAS used for?

A

very important for data science

used for:

  • advanced analytics
  • multivariate analysis
  • business intelligence
  • data management
  • predictive analytics
  • standard for computational analysis
454
Q

How do you import a SAS file?

A
455
Q

How do you import a Stata file?

A
456
Q

What is a HDF5 file and how do you import it?

A
  • Hierarchical Data format Version 5
  • Standard for storing large quantities of numerical data.
    • hundreds of gigabytes or terabytes (can scale to exabytes)
457
Q

what is a MATLAB file and how do you import it?

A
  • Matrix Laboratory
  • Industry standard in engineering and science

scipy. io.loadmat() = read .mat files
scipy. io.savemat() = write .mat files

458
Q

What is a relational database?

A
  • Based on relational model of data
  • Rows are ordered and the columns are attributes for the information in the rows
  • Each row need a primary key column that has a unique entry for each row that we can call information from (such as OrderID, CustomerNumber, EmployeeNumber etc.)
459
Q

How can you create a database engine with SQLAlchemy?

A
460
Q

What does querying mean?

A

get data out of the database

461
Q

How do you create an engine, connect to it, make a basic query, create a database and close it again?

A
  1. Import create_engine
  2. import pandas as pd
  3. Create the engine
  4. Connect to the engine
  5. Write a query (select * => imports all columns)
  6. Turn the query into a DataFrame using fetchall()
  7. Close the connection
462
Q

How do you specify columns that you want to import AND specify the number of rows you want to import in a query + set the DataFrame’s column names to the corresponding names of the table columns?

A
463
Q

How do you make a query that selects all “EmployeeID”s from “Employee” that is greater than or equal to 6?

A
464
Q

How can this be done much smarter with Pandas?

A
465
Q

How do you import data from a url?

A

It is possible to skip the step of saving it locally (line 8-9) and just plug in the url directly in pd.read_csv so:

df = pd.read_csv(url, sep=’;’)

466
Q

What does URL stand for?

A

Uniform/Universal Resource Locator

467
Q

How do you send a GET request to a certain URL and afterwards read the response? (http.client.HTTPResponse)

A
468
Q

How do you make HTTP requests for an URL using the requests package?

A
469
Q

What is the BeautifulSoup package for?

A
  • Parse and extract structured data from HTML
  • “Make tag soup beautiful and extract information”
470
Q

use the BeautifulSoup package to parse, prettify and extract information from HTML.

The URL of interest is url = ‘https://www.python.org/~guido/’.

A
471
Q

What does API mean and what is it?

A
  • Application Programming Interface
  • Protocols and routines that build and interact with software applications
    • allows two software programs to communicate with each other
472
Q

What is JSON?

A
  • JavaScript Object Notation
  • Real-time server-to-browser-communication
  • It is readable by humans
  • loads as a dictionary {}
473
Q

Load the JSON ‘a_movie.json’ into the variable json_data, which will be a dictionary. Next, explore the JSON contents by printing the key-value pairs of json_data to the shell.

A
474
Q

How do you connect to an API?

A
475
Q

Import and print the text from the URL, http://www.omdbapi.com/ with the these two argument in the query string; apikey=72bc447a and t=the+social+network

A
476
Q

Which function do you use to decode a JSON to a dictionary?

A

the json() method

so…

url = ‘https://en.wikipedia.org/w/api.php?action=query&prop=extracts&format=json&exintro=&titles=pizza’

Package the request, send the request and catch the response: r

r = requests.get(url)

Decode the JSON data into a dictionary: json_data

json_data = r.json()

477
Q

What are some of the most common data problems?

A
  • Inconsistent column names
  • Missing data (NaN)
  • Outliers
  • Duplicate rows
  • Untidy
  • Need to process columns
  • Column types can signal unexpected data values
478
Q

What are some of the most important functions in pandas to check out your data?

A
  • df.columns() #to get column names
  • df.head() #first 5 rows by default
  • df.tail() #last 5 rows by default
  • df.shape() #shape of dataframe with (rows,columns)
  • df.info() #various info about the dataframe, non-missing values, data type etc.
  • df.describe() #getting basic statistics ONLY on numeric columns
479
Q

How can you get the frequency counts for each unique value in a column?

A

whether the data is numeric or not, you can use

.value_counts(dropna=False)

dropna=False is important to add to see missing values (NaN)

note that it is value_counts with s

480
Q

What are great plotting tools to inspect your data and its outliers?

A

import matplotlib.pylot as plt

  • df[‘Existing Zoning Sqft’].plot(kind=’hist’, rot=70, logx=True, logy=True) #here setting both x and y axis as logarithmic
  • df.boxplot(column=’initial_cost’, by=’Borough’, rot=90) # comparing inital_cost column across different values of the Borough column
    • great for numeric values to be compared across different categories
  • df.plot(kind=’scatter’, x=’initial_cost’, y=’total_est_fee’, rot=70)
    • great for visiualizing two numeric columns

plt.show()

481
Q

What are the 3 principles of tidy data?

A
  • Columns represent separate variables
  • Rows represent individual observations
  • Observational units form tables

Tidy data makes it easier to fix common data problems

482
Q

What is this doing?

A
  • We are melting columns except ‘month’ and ‘day’
  • we call the value-column = reading
  • and the variable column = measurement

Melting turns columns into rows

483
Q

What does pivoting do?

A

turn unique values into separate columns

why:

  • analysis friendly shape to reporting friendly shape

While melting takes a set of columns and turns it into a single column, pivoting will create a new column for each unique value in a specified column.

484
Q

What is this doing?

A

Pivoting such that

  • we do not change the two columns ‘month’ and ‘day’
  • we change the ‘measurement’ column and split the unique values in this column into new columns
  • and take the values from ‘reading’ and plug them into those rows
485
Q

How would you split a column containing gender and age values in one such as m014, m65, f65, f4554 and so forth?

A
486
Q

How would you split a column with values such as Cases_Guinea, Deaths_Guinea, Cases_Liberia, Deaths_Liberia?

A

_ serves as a delimiter

we can use .split()

487
Q

How do you concatenate data?

A

concatenating ROWS

pd.concat([firstset, secondset, thirdset, etcset], axis=0)

pd.concat([firstset, secondset, thirdset, etcset], axis=1)

NOTE… Data MUST be in a list to concatenate DataFrames.

488
Q

What is globbing about?

A

pattern matching for file names

wildcards: *?

  • any csv file: *.csv
  • any single character: file_?.csv
489
Q

How do you merge data?

A

pd.merge(left=dd, right=dd_d, on=None, left_on=’something’, right_on=’something)

example:

two dataframes; visited and site

with columns that are the same called “site” and “name”

o2o = pd.merge(left=site, right=visited, left_on=’name’, right_on=’site’)

Many-to-many data merge ; The final merging scenario occurs when both DataFrames do not have unique keys for a merge. What happens here is that for each duplicated key, every pairwise combination will be created.

490
Q

How can you see the data types in your DataFrame?

A

print(df.dtypes)

491
Q

How do you convert an object column to categorical?

A

This will lower the memory need

492
Q

How do you convert a column to numeric?

A
493
Q

How can you make the computer interpret $17.00 with escape sequences?

A
494
Q

How can you make the computer interpret $17.89 with escape sequences? #make sure it only takes two decimals for the monetary value

A
495
Q

How do make a pattern matching for a us phone number in the format xxx-xxx-xxxx?

A
496
Q

How can you make python extract the numbers found in a string? ‘the recipe calls for 10 strawberries and 1 banana’

A
497
Q

How could you write a pattern matching sequence to make ‘$123.45’ turn True?

A
498
Q

How can you remove duplicate data?

A

df.drop_duplicates()

Important to delete duplicate values as they take up a lot of memory and cause unneeded calculations to be performed when processing data

499
Q

How can you fill missing values?

A

.fillna()

500
Q

How do you make an assert statement?

A
501
Q

What is this doing?

A
  1. We have a dataset, ‘gapminder’ with 780 rows and 218 columns
  2. each column represent a year with life expectancy data
  3. but the very first column [0] has the country names
  4. we then melt the data such together such that we get rows instead of columns = one row will have a year value, the life expectancy and the associated country
  5. we make sure not to touch the country life by putting ‘life expectancy’ in as id_vars
  6. we then rename the columns to ‘country’, ‘year’ and ‘life_expectancy’
  7. and print the head of it
502
Q

How do you convert the column ‘year’ in the df ‘gapminder’ to numeric AND asset that the change has been made?

A
503
Q

Say you have two lists, ‘list_keys’ and ‘list_values’, how do you create a dictionary and then a dataframe?

A
504
Q

How can you in a structured way rename the columns of your dataframe?

A
505
Q

Say we have a list of cities in Pennsylvania, how can we create a dataframe from this list AND add a column with the value ‘PA’ for Pennsylvania in all rows?

A
506
Q

What are the 3 ways to create a histogram?

A
  1. df.plot(kind=’hist)
  2. iris.plt.hist()
  3. iris.hist()

NOTE they have different syntax

507
Q

How can you get the 5th and 95th percentile of a dataframe?

A

print(df.quantile([0.05, 0.95])

508
Q

How can you downsample hourly data of ‘Temperature’ in df to six hours and to daily?

A
509
Q

Say we have a df with the column ‘Temperature’ and we want to extract all of august 2010 and show the maximum daily temperature?

A
510
Q

How can you convert a column, say ‘wind_speed’ in df to numeric values?

A

df[‘wind_speed’] = pd.to_numeric(df[‘wind_speed’], errors=’coerce’)

511
Q

Filter rows of august_2011 to keep days where the value exceeded august_max. Store the result in august_2011_high.

A

august_2011_high = august_2011.loc[august_2011 > august_max]

loc with boolean condition

both august_2011 and august_max are already encoded as variables

512
Q

What is the difference between selecting df[‘eggs’] and df[[‘eggs’]]?

A

df[‘eggs’] returns a series (one-dimensional labeled data)

df[[‘eggs’]] returns a DataFrame

513
Q

How can you assign a index name and overall column name to a DataFrame,df?

A

df. index.name = ‘MONTHS’
df. columns.name = ‘PRODUCTS’

just as you set the index names by..

df.index = [‘Jan’, ‘Feb’, ‘Mar’, ‘Apr’]

514
Q

Say we have the dataframe, ‘sales’ and want to make both the ‘state’ and the ‘month’ column indexes, what will we have to do?

A

Set the index to be the columns [‘state’, ‘month’]: sales

sales = sales.set_index([‘state’, ‘month’])

Sort the MultiIndex: sales

sales = sales.sort_index()

Print the sales DataFrame

print(sales)

515
Q

Can you pivot a dataframe that has multiindex?

A

No…

you will have to unstack the multi-index by turning an index to a column first.

df.unstack(level=’indexcolumnname’)

or

df.unstack(level=1) #to remove the second index

516
Q

How can you switch around indexes in a multi-index?

A

swapped = df.swaplevel(0, 1)

swapped.sort_index()

517
Q

How can we call a zscore on a dataFrame? FIRST… groupby ‘region’ and columns, ‘life’ and ‘fertility’

A

standardized = gapminder_2010.groupby(‘region’)[‘life’,’fertility’].transform(zscore)

518
Q

How does idxmax() and idxmin() work?

A
  • idxmax()
    • Row or column label where maximum value is located
    • idxmax(axis=’columns) #for columns
  • idxmin()
    • Row or column label where minimum value is located
    • idxmin(axis=’columns) #for columns
519
Q

what type of plot do you get from usa_medals_by_year.plot.area()?

A
520
Q

You may have noticed that the medals are ordered according to a lexicographic (dictionary) ordering: Bronze < Gold < Silver. However, you would prefer an ordering consistent with the Olympic rules: Bronze < Silver < Gold. How can we change the ordering?

A

You can achieve this using Categorical types.

Redefine the ‘Medal’ column of the DataFrame medals as an ordered categorical. To do this, use pd.Categorical()

medals.Medal = pd.Categorical(values=medals.Medal, categories=[‘Bronze’, ‘Silver’, ‘Gold’], ordered=True)

521
Q

Say you have a list of csv files, filenames = [‘Gold.csv’, ‘Silver.csv’, ‘Bronze.csv’], how can we import all these?

A

use a loop….

Create the list of three DataFrames: dataframes

dataframes = []

for filename in filenames:

dataframes.append(pd.read_csv(filename))

522
Q

eu_metro_areas.drop(“High Population”, axis=1, inplace=True)

eu_metro_areas

What is the purpose of inplace = True?

A

inplace = True is by default false.

When True, we change the current dataframe

when not set, and thus false by default, we create a new dataframe with a column missing. so… we get multiple dataframes, which is slower and often quite useless.

523
Q

What will this do?

z.replace({“Nowhere”:”A city”}, inplace=True)

z

A

we are changing the str ‘Nowhere’ to ‘A city’ and overwrite the current DataFrame as we set inplace=True.

524
Q

How do you convert a DataFrame to CSV?

A

eu_metro_areas.to_csv(‘My_metro_areas.csv’, sep=’,’)

525
Q

How do you convert a DataFrame to Excel?

A

eu_metro_areas.to_excel(“My_metro_areas.xlsx”)

526
Q

what is rug=True doing? sns.distplot(eu_metro_areas[“GDP”],bins=7,rug=True)

A

it will show small bars for each observation/datapoint in the graph so we understand how big the dataset is

527
Q

What join does pd.concat take as default?

A

outer

528
Q

How do you add a column to an existing dataframe, df?

A

df[‘newcolumnname’] = [‘list’, ‘of’, ‘values’, ‘for’, ‘new’, ‘column’]

529
Q

When should you use .append(), pd.concat(), df.join() and pd.merge()?

A
  • .append()
    • stacking vertically
  • pd.concat()
    • stacking many horizontally or vertically
    • simple inner/outer joins on indexes
  • df.join()
    • inner, outer, left, right joins on indexes
  • pd.merge()
    • many joins on multiple columns
530
Q

What join does pd.merge_ordered take as default?

A

outer