Back to marketplace

Pandas Primer

No description yet. This curated deck is ready for daily study.

Total cards: 24•Content version: v4•Public deck•Updated: 8/1/2026

Card preview (24)

#1
Front (Question)

How do you read a comma-delimited file in pandas?

Back (Answer)

df = pd.read_csv(filepath)

#2
Front (Question)

How do you create a dataframe in pandas? (2)

Back (Answer)

• pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns = [‘col1’, ‘col2’, ‘col3’]) • With a dictionary: • df = pd.DataFrame({“ID” : [1, 2, 3], “First Name” : [“John”, “Jim”, “Joe”], “Last Name” : [“Smith”, “Hendry”, “Wilson”]})

#3
Front (Question)

How do you show a dataframe in pandas?

Back (Answer)

display(df)

#4
Front (Question)

How do you access a cell in a dataframe?

Back (Answer)

• .loc[‘cobra’] • .iloc[row_or_col_index] • .loc[row_label,col_label] • .iloc[row_index,col_index]

#5
Front (Question)

What selection features does pandas support? (2)

Back (Answer)

• Slicing in .loc and .iloc with start_index:end_index • array indexing

#6
Front (Question)

What’s the difference between .loc and .iloc?

Back (Answer)

• loc is label-based, -> use row and column labels. • iloc is integer position-based -> use integer position values

#7
Front (Question)

how do you show the first few rows of a dataframe?

Back (Answer)

df.head(some_num_rows)

#8
Front (Question)

What’s one interesting thing about pandas slicing?

Back (Answer)

• When you want an entire row or column, instead of including a “:”, can omit the row or column index entirely (along with the comma) by not using a loc or iloc function at all • E.g. • display(df.loc[:, “Last Name”]) is equivalent to display(df[“Last Name”])

#9
Front (Question)

How do you set an entry in a DataFrame?

Back (Answer)

df.loc[1,”Last Name”] = “some_val”

#10
Front (Question)

How do you set a row in pandas?

Back (Answer)

df.loc[3,:] = (100, “Andrew”, “Moore”)

#11
Front (Question)

What happens if you try to set a row and the input index doesn’t exist?

Back (Answer)

new row is appeneded to the end

#12
Front (Question)

How do we select a subset of the rows that satisfy some conditions from a dataframe?

Back (Answer)

df[(df[“First Name”] == “Jim”) & (df[“Last Name”] == “Kilter”)]

#13
Front (Question)

Given this dataframe, how do you find rows where Last Name has 6 characters?

Card front image
Back (Answer)

df[df[“Last Name”].str.len() == 6]

#14
Front (Question)

Given this dataframe, how do you find rows where First Name contains the substring “Jo”?

Card front image
Back (Answer)

df[df[“First Name”].str.contains(“Jo”)]

#15
Front (Question)

Given this dataframe, how do you find rows where First Name is either “Jim” or “Kim”?

Card front image
Back (Answer)

df[df[“First Name”].isin([“Jim”, “Kim”])]

#16
Front (Question)

What do you do if you want to find rows that do not satisfy a certain condition?

Back (Answer)

Use the negation symbol ~ E.g. df[~df[“First Name”].isin([“Jim”, “Kim”])]

#17
Front (Question)

• What’s one trick we can use to speed up the selecting of pandas rows? • What does it do?

Back (Answer)

• Use a query string to select rows, which • df.query(‘(First Name == “John”) & (Last Name == “Smith”)’) • can avoid the creation of the intermediate boolean index and reduce runtime / memory usage:

#18
Front (Question)

What’s important to remember about querying? (3)

Back (Answer)

• The returned object of a query is a view of the original data frame. • Modifying the view will not affect the original data frame, but will yield a warning. • Unlike Numpy, Pandas preserves the original row index after filtering. E.g. if you make a copy of a slice that has rows 2 and 5 and try to select index 1 from that, an error will be thrown.

#19
Front (Question)

How do you copy a dataframe?

Back (Answer)

.copy()

#20
Front (Question)

If our dataframe has no row with index 0, what do we do?

Back (Answer)

Call .reset_index(drop = True) E.g. df_copy_reset_index = df_copy.reset_index(drop = True)

#21
Front (Question)

How can we iterate over rows of a dataframe, from slowest to fastest?

Back (Answer)

• Use .iloc along with row index. • Use iterrows method. • Use apply with axis=1. • Fastest: Use Pandas vectorization

Card back image
#22
Front (Question)

How can we iterate over columns of a dataframe? (iteration syntax and how you index the column)

Back (Answer)

• Call .columns to get the list of column names and iterate over it • Use .iloc along with the column indexes

Card back image
#23
Front (Question)

What’s important to note about iterrows()?

Back (Answer)

• the row returned is only a copy of the data • so you cannot update the data frame during .iterrows()

#24
Front (Question)

What is the default of the apply method?

Back (Answer)

• To loop through the columns (same as axis=0) • To loop through rows, use axis=1