Operations and Data Manipulation Flashcards

1
Q

How do you iterate through a DataFrame row by row?

A

Use df.iterrows() to iterate through rows as (index, Series) pairs.

for index, row in df.iterrows():
print(f”Index: {index}, Row: {row[‘A’]}”)

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

What is a list comprehension, and how can it be used with DataFrames?

A

A list comprehension creates a list by iterating over an iterable in a concise way.

df[‘A_squared’] = [x**2 for x in df[‘A’]]
print(df)

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

How do you perform an operation on multiple DataFrame columns efficiently?

A

Use vectorized operations for efficiency.

df[‘Sum’] = df[‘A’] + df[‘B’]
print(df)

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

How do you filter rows based on a condition?

A

Use boolean indexing with a condition.

filtered_df = df[df[‘A’] > 1]
print(filtered_df)

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

How do you use .apply() to apply a function to a column?

A

Use df[‘column’].apply(func) to apply a function to each element in a column.

df[‘A_doubled’] = df[‘A’].apply(lambda x: x * 2)
print(df)

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

How do you create a new column based on multiple columns?

A

Use a vectorized operation or .apply() with axis=1.

df[‘Sum’] = df.apply(lambda row: row[‘A’] + row[‘B’], axis=1)
print(df)

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

How do you loop through a dictionary and apply operations to DataFrame columns?

A

Use a loop to iterate through the dictionary keys and apply functions.

operations = {‘A’: lambda x: x**2, ‘B’: lambda x: x + 10}
for col, func in operations.items():
df[col] = df[col].apply(func)
print(df)

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

How do you compute the cumulative sum of a column?

A

Use df[‘column’].cumsum().

df[‘Cumulative_A’] = df[‘A’].cumsum()
print(df)

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

How do you find the unique values in a column?

A

Use df[‘column’].unique().

unique_vals = df[‘A’].unique()
print(unique_vals)

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

How do you count occurrences of each unique value in a column?

A

Use df[‘column’].value_counts().

counts = df[‘A’].value_counts()
print(counts)

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

How do you use .map() to transform values in a column?

A

Use df[‘column’].map(mapping_dict) or map(func).

mapping = {1: ‘One’, 2: ‘Two’}
df[‘Mapped_A’] = df[‘A’].map(mapping)
print(df)

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

How do you create a column by combining string columns?

A

Use df[‘col1’] + df[‘col2’] for concatenation.

df[‘FullName’] = df[‘FirstName’] + ‘ ‘ + df[‘LastName’]
print(df)

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

How do you use .applymap() to apply a function to all elements in a DataFrame?

A

Use df.applymap(func) for element-wise transformations.

df = df.applymap(lambda x: x * 2)
print(df)

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

How do you drop specific columns from a DataFrame?

A

Use df.drop(columns=[‘col1’, ‘col2’]).

df = df.drop(columns=[‘B’])
print(df)

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

How do you add a column with a default value?

A

Use df[‘new_column’] = default_value.

df[‘New_Col’] = 0
print(df)

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