Operations and Data Manipulation Flashcards
How do you iterate through a DataFrame row by row?
Use df.iterrows() to iterate through rows as (index, Series) pairs.
for index, row in df.iterrows():
print(f”Index: {index}, Row: {row[‘A’]}”)
What is a list comprehension, and how can it be used with DataFrames?
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 do you perform an operation on multiple DataFrame columns efficiently?
Use vectorized operations for efficiency.
df[‘Sum’] = df[‘A’] + df[‘B’]
print(df)
How do you filter rows based on a condition?
Use boolean indexing with a condition.
filtered_df = df[df[‘A’] > 1]
print(filtered_df)
How do you use .apply() to apply a function to a column?
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 do you create a new column based on multiple columns?
Use a vectorized operation or .apply() with axis=1.
df[‘Sum’] = df.apply(lambda row: row[‘A’] + row[‘B’], axis=1)
print(df)
How do you loop through a dictionary and apply operations to DataFrame columns?
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 do you compute the cumulative sum of a column?
Use df[‘column’].cumsum().
df[‘Cumulative_A’] = df[‘A’].cumsum()
print(df)
How do you find the unique values in a column?
Use df[‘column’].unique().
unique_vals = df[‘A’].unique()
print(unique_vals)
How do you count occurrences of each unique value in a column?
Use df[‘column’].value_counts().
counts = df[‘A’].value_counts()
print(counts)
How do you use .map() to transform values in a column?
Use df[‘column’].map(mapping_dict) or map(func).
mapping = {1: ‘One’, 2: ‘Two’}
df[‘Mapped_A’] = df[‘A’].map(mapping)
print(df)
How do you create a column by combining string columns?
Use df[‘col1’] + df[‘col2’] for concatenation.
df[‘FullName’] = df[‘FirstName’] + ‘ ‘ + df[‘LastName’]
print(df)
How do you use .applymap() to apply a function to all elements in a DataFrame?
Use df.applymap(func) for element-wise transformations.
df = df.applymap(lambda x: x * 2)
print(df)
How do you drop specific columns from a DataFrame?
Use df.drop(columns=[‘col1’, ‘col2’]).
df = df.drop(columns=[‘B’])
print(df)
How do you add a column with a default value?
Use df[‘new_column’] = default_value.
df[‘New_Col’] = 0
print(df)