4th Flashcards

1
Q

Query results

A

df.query(‘statement’)

ex.

stocks. query(‘price >= 90’)
stocks. query((‘stock == “disney” or (stock == “nike” and price < 90’))

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

Reshape data from wide to long

A

df.melt(id_vars = [‘c’, ‘c1’]), value_vars = [‘val’, ‘val1’], var_name = [‘var_name’], value_name = ‘val_name’)

id_vars: columsn not to change

value_vars = the values columns to keep

value_name = the name of the value column

ex

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

Set up MatPlotLib

A

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

plt.show()

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

Single line plot Matplotlib

A

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

ax. plot(df[‘x’], df[‘y’])
plt. show()

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

Multiple line plots on a single Matplotlib

A

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

ax. plt(df[‘x’], df[‘y’])
ax. plt(df1[‘x’], df1[‘y’])
plt. show()

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

Add marker to line plot in Matplotlib

A

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

ax. plot(df[‘x’], df[‘y’], marker = ‘o’)
plt. show()

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

Change linestyle to dashed Matplotlib

A

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

ax. plot(df[‘x’], df[‘y’], marker = ‘v’, linestyle = ‘–’)
plt. show()

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

Choose color in Matplotlib

A

import matplotlib.pyplot as plt

ax, fig = plt.subplots()

ax.plot(df[‘x’], df[‘y’], color = ‘r’)

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

Customize axes labels Matplotlib

A

ax. set_xlabel(‘x label’)
ax. set_ylabel(‘y label’)
plt. show()

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

Add title Matplotlib

A

ax. set_title(‘title’)
plt. show()

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

Create multiple rows/columns Matplotlib

A

import matplotlib.pyplot as plt

ax, fig = plt.subplots(3, 2)

plt.show()

(# of row, # of col)

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

Add graph to a multiple row/col Matplotlib

A

import matplotlib.pyplot as plt

ax, fig = plt.subplots(3, 2)

ax[0, 0].plot(df[‘x’], df[‘y’], color = ‘b’)

plt.show()

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

Plot multiple lines (avg, max) on multiple graphs (df, df1) in Matplotlib

A

import matplotlib.pyploy as plt

fig, ax = plt.subplots(2, 1)

ax[0].plt(df[‘x’], df[‘avg_y’], color = ‘b’)

ax[0].plt(df[‘x’], df[‘max_y’], linestyle = ‘–’, color = ‘b’)

ax[1].plt(df[‘x’], df1[‘avg_y’], color = ‘r’)

ax[1].plt(df[‘x’], df1[‘max_y’], linestyle = ‘–’, color = ‘r’)

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

Bar chart in Matplotlib

A

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

ax. bar(df[‘cat’], df[‘val’])
ax. set_xticklabels(df[‘cat’], rotation = 90)
ax. set_ylabel(‘y label’)
plt. show()

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

Stacked bar chart with 2 bars in Matplotlib

A

import matplotlib.pyploy as plt

fig, ax = plt.subplots()

ax. bar(df[‘cat’], df[‘val’])
ax. bar(df[‘cat’], df[‘val2’], bottom = df[‘val’])
ax. set_xticklabels(df[‘cat’], rotation = 90)
ax. set_ylabel(‘y label’)
plt. show()

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

Stacked barchart 3 bars Matplotlib

A

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

ax. bar(df[‘cat’], df[‘var1’])
ax. bar(df[‘cat’], df[‘var2’], bottom = df[‘var1’])
ax. bar(df[‘cat’], df[‘var3’], bottom = df[‘var1’] + df[‘var2’])
ax. setxticktablels(df[‘cat’], rotation = 90)
ax. set_ylabel(‘Y Label”)
ax. legend()
plt. show()

17
Q

Compare between two histograms Matplotlib

A

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

ax. hist(df[‘col’], label = ‘COL’, bins = #)
ax. hist(df[‘col2’], label = ‘COL2’, bins = #))
ax. set_xlabel(‘XLABEL’)
ax. set_ylabel(‘YLABEL’)
ax. legend()
plt. show()

18
Q

Compare between two histograms and set bin boundaries in Matplotlib

A

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

ax. hist(df[‘col’], label = ‘COL’, bins = [#, #, #, #, #, #, #, #, #])
ax. hist(df[‘col2’], label = ‘COL2’, bins = [#, #, #, #, #, #, #, #, #])
ax. set_xlabel(‘XLABEL’)
ax. set_ylabel(‘YLABEL’)
ax. legend()
plt. show()

19
Q

Compare between two histograms and set it to transparent in Matplotlib

A

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

ax. hist(df[‘col’], label = ‘COL’, histtype = ‘step)
ax. hist(df[‘col2’], label = ‘COL2’, histtype = ‘step)
ax. set_xlabel(‘XLABEL’)
ax. set_ylabel(‘YLABEL’)
ax. legend()
plt. show()

20
Q

Adding error bars to line plots in Matplotlib

A

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

ax. errorbar(df[‘x’], df[‘y’], yerr = df[‘y’])
ax. errorbar(df2[‘x’], df2[‘y’], yerr = df2[‘y’])
ax. set_ylabel(‘YLABEL’)
plt. show()

21
Q

Boxplots in Matplotlib

A

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

ax. boxplot([df1[‘val’], df2[‘val’]])
ax. set_xticklabels([‘df val name’, ‘df2 val name’])
ax. set_ylabel(‘y label’)
plt. show()

22
Q

Scatter plot in matplotlib

A

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

ax. scatter(df[‘x’], df[‘y’], color = ‘red’, label = ‘c’)
ax. legend()
ax. set_xlabel(‘x label’)
ax. set_ylabel(‘y label’)
plt. show()

23
Q

Multiple Scatter plots

A

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

ax. scatter(df[‘x’], df[‘y’], color = ‘red’, label = ‘c’)
ax. scatter(df1[‘x’], df1[‘y’], color = ‘blue’, label = ‘c1’)
ax. legend()
ax. set_xlabel(‘x label’)
ax. set_ylabel(‘y label’)
plt. show()

24
Q

Saving visualizations Matplotlib

A

fig.savefig(‘location/file.png’)

25
Q

Saving visualizaitons using dpi Matplotlib

A

fig.savefig(‘location/file.png’, dpi = 300)

26
Q

Saving visualizations in Matplotlib using quality

A

fig.savefig(‘location/file.png’, quality = 50)

27
Q

Save visualization as a vector in matplotlib

A

fig.savefig(‘location/file.svg’)