pandas Flashcards

(10 cards)

1
Q

Filter rows where column > value

A

df[df[‘col’] > x]

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

Select rows with multiple conditions

A

df[(df[‘a’] > 0) & (df[‘b’] == ‘x’)]

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

Create a new column

A

df[‘total’] = df[‘a’] + df[‘b’]

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

Group by and aggregate

A

df.groupby(‘cat’)[‘val’].mean()

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

Count rows per group

A

df.groupby(‘cat’).size()

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

Fill missing values

A

df[‘x’].fillna(df[‘x’].median())

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

Sort by multiple columns

A

df.sort_values([‘a’,’b’], ascending=[True,False])

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

Drop duplicates

A

df.drop_duplicates(subset=’id’)

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

Apply function row-wise

A

df[‘y’] = df[‘x’].apply(f)

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

Merge DataFrames

A

df1.merge(df2, on=’id’, how=’left’)

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