Pandas Flashcards

(27 cards)

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

How do you import the pandas library with alias?

A

import pandas as pd

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

How do you create a DataFrame from a list of lists?

A

pd.DataFrame(data, columns=[‘col1’, ‘col2’])

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

How do you convert a column to float type in a DataFrame?

A

df[‘column’].astype(‘float’, copy=False)

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

How do you view the data types of all columns in a DataFrame?

A

df.dtypes

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

How do you create a DataFrame from a dictionary?

A

pd.DataFrame({‘col1’: […], ‘col2’: […]})

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

How do you create a DataFrame using zip()?

A

pd.DataFrame(list(zip(list1, list2)), columns=[‘col1’, ‘col2’])

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

How do you view the first 5 rows of a DataFrame?

A

df.head()

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

How do you view the last 5 rows of a DataFrame?

A

df.tail()

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

How do you select a single column from a DataFrame?

A

df[‘column’] or df.column

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

How do you filter rows where Age > 25?

A

df[df[‘Age’] > 25]

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

How do you filter with multiple conditions (Age > 25 and Grade == ‘A’)?

A

df[(df[‘Age’] > 25) & (df[‘Grade’] == ‘A’)]

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

How do you add a new column to a DataFrame with a scalar value?

A

df[‘new_column’] = value

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

How do you add a new column to a DataFrame with a list of values?

A

df[‘new_column’] = [val1, val2, …]

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

How do you rename the column of a Series?

A

series.name = ‘NewName’

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

How do you rename the index of a Series?

A

series.index.name = ‘IndexName’

17
Q

How do you drop a column from a DataFrame?

A

df.drop(‘column’, axis=1)

18
Q

How do you drop a row by index from a DataFrame?

A

df.drop(index_label)

19
Q

How do you reset the index of a DataFrame?

A

df.reset_index(drop=True)

20
Q

How do you sort a DataFrame by column values?

A

df.sort_values(‘column’)

21
Q

How do you check for null (missing) values?

22
Q

How do you drop rows with null values?

23
Q

How do you fill null values with a default?

A

df.fillna(default_value)

24
Q

How do you get the summary statistics of a DataFrame?

A

df.describe()

25
How do you concatenate two DataFrames?
pd.concat([df1, df2], ignore_index=True)
26
How do you access a specific cell (row i, column j)?
df.iloc[i, j]
27
How do you access data by label (row 'Ohio', column 'population')?
df.loc['Ohio', 'population']