Pandas Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

PD: In normal Pandas style, what is a row and what is a column?

A

A row is an item or point, and a column is a field or feature.

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

PD: Why do most pandas methods return a df rather than editing the original df in place?

A

It allows for method chaining, or running several methods in a nest, which improves readability of code.

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

PD: Do most pandas methods edit a df in place, or return a new df?

A

Return a new df

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

PD: How do you read a table or tables from a web page?

A

pd.read_html(‘sampleurl.com’,…)

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

PD: How do you read a table from a .csv?

A

pd.read_csv(\path, sep=” “,…)

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

PD: How to combine the rows of 2 dfs with the same column headers?

A

pd.concat([df1,df2])

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

PD: How to view the first 5 rows?

A

df.head()

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

PD: How to view the last 5 rows?

A

df.tail()

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

PD: How do we remove columns that aren’t useful to us from our dataframe?

A

to_drop = [“all”,”col”,”names”,”to”,”drop”]

df.drop(columns=to_drop, inplace=True)

(could remove inplace and do a df=df.drop())

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

PD: What are the two ways to exclude columns that aren’t useful to us

A

Drop them using df.drop(), or don’t import then from the csv using inputs to pd.read_csv()

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

PD: If I go df[“a”], what data structure comes out?

A

A pandas series

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

PD: How to I tell if column “A” in a dataframe contains all unique values?

A

df[“A”].is_unique

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

PD: How do I set a new column, such as column “A”, to be the index of a dataframe

A

df = df.set_index[“A”]

If you want a unique index for row names, first check that the column contains unique values using df[“A”].is_unique

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

PD: What is a convenient way to standardize a column containing multiple formats or data types?

A

Regular expressions, or regex.

See the Pythonic Data Cleaning article for an example: https://realpython.com/python-data-cleaning-numpy-pandas/

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

PD: How to find a series containing booleans for whether each value in column “A” is null or not in a dataframe?

A

df[“A”].isnull()

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

PD: How to find the total number of nulls in column “A” in a dataframe?

A

df[“A”].isnull().sum()

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

PD: How to rename cols in a dataframe?

A

df.rename(columns=listOfNewColNames, inplace=True)

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

PD: What does it mean to do a split-apply-combine in pandas?

A

Say the rows of our dataframe fall into a few different “categories,” and we’d like to find a summary statistic or statistics within each of the categories. We can separate all of the categories into groups (split), then apply one or more summary functions to each group (apply), and lastly combine the summary statistics for each group into a new data frame where each row is now one of the categories (combine).

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

PD: How can we, in one line of code, do all 3 parts of split-apply-combine?

A

df.groupby(“colNameToGroupBy”)[“colNameToFindSummaryStatsOn”] .agg([“list”,”of”,”summary”,”stat”,”functions”])

Video for reference: https://www.youtube.com/watch?v=qy0fDqoMJx8

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

PD: How to sort the rows of a data frame based on column “A”, going from low to high

A

df.sort_values(“A”) (ascending, or low to high, is default)

21
Q

PD: How to sort the rows of a data frame based on column “A”, going from high to low

A

df.sort_values(“A”, ascending=False)

22
Q

PD: How to reset indices of DataFrame to row numbers, moving the index to a new column.

A

df.reset_index()

23
Q

PD: How do we display the data types of each column in our df?

A

df.dtypes

24
Q

PD: How to sample a subset of rows?

A

df.sample()

(See details for info on syntax and replacement
)

25
Q

PD: How to extract specific rows meeting a logical criterion?

A

df[(array of logical bools)]

For specifics and examples, see cheat sheet

26
Q

PD: How to get n rows with the largest values of a specific column?

A

df.nlargest(n,”nameOfColumn”)

27
Q

PD: How to get n rows with the smallest values of a specific column?

A

df.nsmallest(n,”nameOfColumn”)

28
Q

PD: What are the 2 ways to get a column from a df, given that the column name has no spaces or special characters

A

df[“colName”] OR df.colName

29
Q

PD: How to select multiple specific columns from a dataframe?

A

df[ [“colName1” ,”colName2” ,”colName3”] ]

30
Q

PD: How, at a high level, can we pick several columns each matching specific criteria?

A

Regular expressions. (For how, see cheat sheet and also study regexes)

31
Q

PD: What is the high-level difference between loc and iloc?

A

Loc takes a subset of rows, columns or both using labels.

iloc takes a subset of rows, columns or both using positions.

32
Q

PD: Would iloc more commonly be used for subsetting rows or columns? Why?

A

Rows. iloc subsets by position, which would be more common for rows, as we might want rows 10 through 20, for example.

(But it’s worth noting that head() and tail() can also do a lot of this functionality.)

33
Q

PD: Would loc more commonly be used for subsetting rows or columns? Why?

A

Columns. Loc subsets by label, which would be more common for columns, which typically have column names as labels.

34
Q

PD: How to find the number of unique values in columnA of a df?

A

df[“A”].nunique()

35
Q

PD: How to display the number of appearances for each unique value in column A of a df?

A

df[“A”].value_counts()

36
Q

PD: How to display the dimensions of a dataframe?

A

df.shape

37
Q

PD: How to display the number of rows in a dataframe without using df.shape?

A

len(df)

38
Q

PD: How to get basic summary statistics for each column in a df?

A

df.describe()

39
Q

PD: How to get the mean, or max, or sum, of column A of a df? Works for lots of summary stats?

A

df[“A”].mean() or df[“A”].max() or df[“A”].sum()

40
Q

PD: How to drop all rows where any column has NAs or null data?

A

df.dropna()

41
Q

PD: How to fill in all NAs with a value?

A

df.fillna(value)

42
Q

PD: How do we group our df by column A, and then find the mean value of column B within each group?

A

df.groupby(“A”)[“B”].mean()

(So you can use the [“B”] notation, or .B notation too, to extract that column for all groups, and then an aggregation function like mean() to run the aggregation for all groups. This makes the syntax very similar to if you were finding the mean of B in a single df rather than a groupby object)

43
Q

PD: How to get the size of each group in groupby object g?

A

g.size()

44
Q

PD: How to make a new column C in df that is the product of columns A and B?

A

df[“C”] = df[“A”] *df[“B”]

45
Q

PD: At a high level, how to descretize a column into several equal-sized buckets based on quantiles?

A

qcut()

46
Q

PD: Given 2 data frames adf and bdf with common column A, what is the general syntax to do a join on column A, be it a left outer, right outer, inner or full outer join?

A

pd.merge(adf, bdf, how = “type”, on=”A”)

where type is “left”, “right”, “inner” or “outer”, corresponding to the 4 join types we learned in SQL.

47
Q

PD: Given 2 data frames adf and bdf with common column A, how do we find all rows in adf with a value in column A that is also present in bdf?

A

adf[adf.A.isin(bdf.A)]

48
Q

PD: Given 2 data frames adf and bdf with common column A, how do we find all rows in adf with a value in column A that is NOT present in bdf?

A

adf[~adf.A.isin(bdf.A)]

49
Q

PD: How does pandas store strings?

A

Pandas uses the object dtype for storing strings, not the str dtype!