groupby, agg, merge and join types, concat, pivot, melt, apply, datetime handling and method chaining.
40 cards · basic cards · AI-written, checked twice. Edit anything.
- What does groupby() do?
Show answer
Splits a DataFrame into groups based on one or more columns, allowing separate computation on each group.
- What is the basic syntax for groupby in pandas?
Show answer
df.groupby('column_name')
- How do you group by multiple columns in pandas?
Show answer
df.groupby(['col1', 'col2'])
- What is the difference between size() and count() after groupby?
Show answer
size() counts all rows including NaN, count() counts non-null values only.
- How do you access a single group by name from a GroupBy object?
Show answer
grouped_obj.get_group('group_name')
- What does reset_index() do after groupby aggregation?
Show answer
Converts the grouped index back to a regular column, flattening the result to a regular DataFrame.
- What is the purpose of agg() in pandas?
Show answer
Applies one or more aggregation functions to grouped data or a DataFrame.
- How do you apply a single aggregation function with agg()?
Show answer
df.groupby('col').agg('sum') or df.groupby('col').agg(np.sum)
- How do you apply different functions to different columns with agg()?
Show answer
df.groupby('col').agg({'col1': 'sum', 'col2': 'mean'})
- How do you apply multiple aggregation functions to the same column with agg()?
Show answer
df.groupby('col').agg({'col1': ['sum', 'mean', 'std']})
- What is the named aggregation syntax in pandas?
Show answer
df.groupby('col').agg(new_col=('old_col', 'sum'))
- What does merge() do?
Show answer
Combines two DataFrames based on a common column or index, similar to SQL joins.
- What is an inner join?
Show answer
Returns only rows where the key exists in both DataFrames.
- What is a left join?
Show answer
Returns all rows from the left DataFrame and matching rows from the right DataFrame.
- What is a right join?
Show answer
Returns all rows from the right DataFrame and matching rows from the left DataFrame.