Data Manipulation With Pandas and Numpy Flashcards

1
Q

How do you access a specific column from a dataframe?

A

df[‘Column1’]

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

How do you create a Pandas Dataframe from a dictionary?

A

data = {‘Column1’: [1, 2, 3], ‘Column2’: [4, 5, 6]}
df = pd.DataFrame(data)

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

How do you filter a dataframe where ‘Column1’ is greater than 2?

A

filtered_df = df[df[‘Column1’] > 2]

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

How do you fill missing values with the mean of a column?

A

df[‘Column1’].fillna(df[‘Column1’].mean(), inplace=True)

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

How do you group data in a dataframe by a specific column and calculate the mean for each group?

A

grouped_df = df.groupby(‘Column1’).mean()

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

How do you apply a function to every element in a dataframe column?

A

df[‘Column1’] = df[‘Column1’].apply(np.square)

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

How do you create a numpy array filled with zeros?

A

zeros_array = np.zeros((3, 3))

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

How do you access the element at the second row, third column of a NumPy array?

A

element = array[1, 2]

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

How do you drop a specific column from a dataframe?

A

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

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

How do you rename a column in a dataframe?

A

df = df.rename(columns={‘OldName’: ‘NewName’})

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

How do you concatenate two dataframes along rows (vertically)?

A

df_combined = pd.concat([df1, df2], axis=0)

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

How do you drop rows with missing values?

A

df_clean = df.dropna()

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

How do you access a specific row in a df?

A

row = df.iloc[3] # Accesses the 4th row (index 3)

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