pandas
98 flashcards by MentisLibrary
Working with tabular data in Python: DataFrames and Series, label and boolean selection, missing data, groupby aggregation, merges and reshaping, time series, file IO, and the vectorised habits that keep large frames fast.
- What is a pandas DataFrame?
- A two-dimensional labelled table where every column is a Series and all columns share one row index.
- What is a pandas Series?
- A one-dimensional labelled array holding a single dtype. Every DataFrame column is a Series.
- How do you look at the first few rows of a table?
- Call df.head(n), which returns the first n rows and defaults to 5.
- What does df.shape return?
- A tuple of (number of rows, number of columns).
- What does df.info() report?
- Column names, the non-null count per column, each dtype, and approximate memory use.
- Which method gives count, mean, quartiles and max in one call?
- df.describe(), which summarises the numeric columns by default.
- How do you see the type of every column?
- Read the df.dtypes attribute, a Series mapping column name to dtype.
- How do you rename columns?
- Pass a mapping to df.rename(columns=...), which returns a new DataFrame.
- How do you drop a column?
- Use df.drop(columns=['col']).
- How do you add a computed column?
- Assign to a new column name using vectorised arithmetic on existing columns.
- Difference between df2 = df and df2 = df.copy()
- The first binds a second name to the same object, so edits show up in both. The second makes an independent copy.
- What is the index of a DataFrame?
- The array of row labels used to align data in every operation.
- How do you turn the index back into a normal column?
- Call df.reset_index(), or df.reset_index(drop=True) to discard it.
- How do you sort rows by a column?
- Use df.sort_values('sales', ascending=False).
- How do you take a random subset of rows?
- Call df.sample(n) for a count or df.sample(frac=0.1) for a proportion.
- When do you use .loc and when .iloc?
- .loc selects by label, .iloc selects by integer position.
- How do you filter rows with a condition?
- Index the frame with a boolean Series of the same length.
- How do you combine two filter conditions?
- Use & for and, | for or, and wrap each condition in parentheses.
- What does df.query() do?
- Filters rows using a condition written as a string, which can be easier to read for long expressions.
- How do you keep rows whose value is in a list?
- Use Series.isin(values).
- How do you filter to a numeric range?
- Use Series.between(low, high), which includes both endpoints by default.
- How do you invert a boolean mask?
- Prefix it with ~.
- How do you select all numeric columns?
- Use df.select_dtypes(include='number').
- Which accessors read or write a single cell fastest?
- df.at[label, column] for labels and df.iat[row, col] for positions.
- Why is df['a'][0] = 1 unreliable?
- It is chained indexing: the first lookup can return a copy, so the assignment may update a temporary and leave df unchanged.
- How do you select a range of columns by name?
- Slice with .loc on the column axis.
- How do you get the top five rows by a column?
- Use df.nlargest(5, 'sales'), and nsmallest for the other end.
- How do you get the label of the row with the maximum value?
- Use Series.idxmax(), and idxmin() for the minimum.
- How do you filter rows whose text contains a substring?
- Use the .str accessor with contains.
- How do you count missing values per column?
- Chain df.isna().sum().