PANDAS Flashcards

1
Q

TIME SERIES

sports = {‘Archery’: ‘Bhutan’,
‘Golf’: ‘Scotland’,
‘Sumo’: ‘Japan’,
‘Taekwondo’: ‘South Korea’}

Can you create a time series from a dictionary? How do you do it?

A
#import pandas and use pd.Series(Dict)
import pandas as pd
s = pd.Series(sports)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

SERIES QUERYING

sports = {‘Archery’: ‘Bhutan’,
‘Golf’: ‘Scotland’,
‘Sumo’: ‘Japan’,
‘Taekwondo’: ‘South Korea’}

s = pd.Series(sports)

a) Retrieve the value associated to index label “Sumo”
b) Retrieve the for the item at idex position 2

A
# Use s.loc[ ] and s.iloc[ ]
print(s.loc["Golf"])
print(s.iloc[2])
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

SHOWING A SAMPLE FROM SERIES

Create a series with a thoundsand random integers, then print the first an last elements in the series.

A
#use numpy.random.rand()
import pandas as pd
import numpy as np

s = pd.Series(np.random.randint(0,1000,1000))
print(“First elements”)
print(s.head())

print(“Last elements”)
print(s.tail())

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

REPEATED INDEXES

original_sports = pd.Series({‘Archery’: ‘Bhutan’,
‘Golf’: ‘Scotland’,
‘Sumo’: ‘Japan’,
‘Taekwondo’: ‘South Korea’})

cricket_loving_countries = pd.Series([‘Australia’,
‘Barbados’,
‘Pakistan’,
‘England’],
index=[‘Cricket’,
‘Cricket’,
‘Cricket’,
‘Cricket’])
full_series= original_sports.append(cricket_loving_countries)

How can you return all the cricket_loving countries?

A

Use the .iloc[ ] property

cricket_countries = full_series.loc[“Cricket”]

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

INDEX AND COLUMN LABELS

How can you retrieve the labels of the index and columns of a data frame?

A
# Use .index and .colums properties
idx = df.index
col = df.columns
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

DATA QUERYING

Explain the workings of .loc[ ] and .iloc[ ] properties

A

df. loc[ ] -> label based querying

df. iloc[ ] -> index based querying

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