subset observations (Rows) and subset variables (columns) Flashcards

1
Q

Extract rows that meet logical criteria.

A

df[df.Length > 7]

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

Remove duplicate rows (only considers columns).

A

df.drop_duplicates()

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

Select first n rows.

A

df.head(n)

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

Select last n rows.

A

df.tail(n)

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

Randomly select fraction of rows.

A

df.sample(frac=0.5)

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

Randomly select n rows.

A

df.sample(n=10)

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

Select rows by position.

A

df.iloc[10:20]

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

Get the rows of a DataFrame sorted by the n largest values of columns.

A

DataFrame.nlargest(n, columns, keep=’first’)

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

Get the rows of a DataFrame sorted by the n smallest values of columns.

A

DataFrame.nlargest(n, columns, keep=’first’)

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

Select multiple columns with specific names.

A

df[[‘width’,’length’,’species’]]

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

Select single column with specific name.

A

df[‘width’] or df.width

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

Select columns whose name matches regular expression regex.

A

df.filter(regex=’regex’)

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

Select all columns between x2 and x4 (inclusive).

A

df.loc[:,’x2’:’x4’]

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

Select columns in positions 1, 2 and 5 (first column is 0).

A

df.iloc[:,[1,2,5]]

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

Select rows meeting logical condition, and only the specific columns .

A

df.loc[df[‘a’] > 10, [‘a’,’c’]]

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