NEW
Font size
WorksheetsPandas and Data Manipulation Quiz 3 часть
Total questions: 29
Worksheet time: 15mins
Output of this code? import pandas as pd df = pd.read_csv('data.csv', skip_blank_lines=True)
Ignores blank lines in the CSV
Reads blank lines as NaN
Error
Deletes CSV
What is the output of this code? import pandas as pd df = pd.read_csv('data.csv', low_memory=False)
Prevents dtype guessing and ensures proper memory usage
Reads CSV in chunks
Converts everything to string
Error
What does this code do? import pandas as pd df = pd.read_sql('SELECT * FROM table1', conn, index_col='ID')
Reads SQL table and sets ‘ID’ as index
Writes SQL table
Converts SQL table to CSV
Creates a new SQL table
What does this code do? import pandas as pd df = pd.read_csv('data.csv', header=None)
Reads CSV without using the first row as header
Reads CSV using the first row as header
Reads only the first row
Writes CSV without header
How do you remove rows with all NaN values? df.dropna(how='all', inplace=True)
Drops rows where all values are NaN
Drops any row with NaN
Drops column with NaN
Fills NaN
How can you strip special characters from a string column? df['col'] = df['col'].str.replace('[^a-zA-Z0-9]', '', regex=True)
Removes all non-alphanumeric characters
Converts to lowercase
Converts to uppercase
Replaces spaces with underscores
Output of IQR outlier filtering code?
Filters out outliers based on IQR
Keeps only outliers
Drops all rows
Error
How do you replace all infinite values with NaN? import numpy as np df.replace([np.inf, -np.inf], np.nan, inplace=True)
Replaces +inf/-inf with NaN
Drops infinite values
Converts to zero
Raises error
What does this code do? df['col'] = df['col'].str.lower()
Converts all strings in the column to lowercase
Converts to uppercase
Strips spaces
Deletes column
How can you remove columns with more than 50% missing values? df.dropna(thresh=len(df)*0.5, axis=1, inplace=True)
Drops columns with more than 50% NaN
Drops rows with >50% NaN
Replaces NaN with 0
Keeps only rows with >50% non-NaN
What does this code produce? pd.concat([df1, df2], axis=0, ignore_index=True)
Stacks df1 and df2 vertically and resets the index
Stacks horizontally
Performs a merge
Produces an error
What is the result of this code? pd.melt(df, id_vars=['X'])
Keeps ‘X’ fixed and unpivots ‘Y’ into long format
Keeps ‘Y’ fixed and unpivots ‘X’
Drops column ‘Y’
Produces an error
What does this code do? pd.merge(df1, df2, how='outer', on='key')
Outer join: keeps all keys from both, fills missing with NaN
Left join
Inner join
Right join
Output of pivot code (long to wide)?
Reshapes from long to wide format with ‘id’ as index
Reshapes from wide to long
Drops column ‘value’
Produces an error
What is the difference between merge and concat?
merge joins based on columns/keys, concat stacks along axis
merge concatenates, concat joins
Both do the same thing
merge deletes duplicates, concat does not
How can you reorder levels in a MultiIndex? df.reorder_levels([1,0])
Switches the positions of the levels
Sorts the index
Drops a level
Creates a new column
What is the key difference between reorder_levels() and swaplevel()?
reorder_levels() allows arbitrary reordering, swaplevel() only swaps two levels
swaplevel() can reorder multiple, reorder_levels() swaps
reorder_levels() on non-hierarchical, swaplevel() cannot
swaplevel() requires all levels
In a merge(), what does how=‘outer’ do?
Performs a union of keys from both DataFrames
Only common keys
Left only
Right only
Output of df.loc[‘A’] in MultiIndex?
Selects all rows where first level is ‘A’
Second level ‘A’
Columns named ‘A’
Error
How do you rotate y-axis tick labels? plt.yticks(rotation=90)
Rotates y-axis labels by 90 degrees
Rotates x-axis
Rotates plot
Error
How do you change bar width in a bar plot? df['A'].value_counts().plot.bar(width=0.3)
Sets bar width to 0.3
Sets spacing
Changes color
Produces an error
How do you plot a histogram with normalized frequencies? df['A'].plot.hist(density=True)
Plots histogram normalized to form a probability density
Plots raw counts
Plots line plot
Produces an error
Which parameters control the spacing between subplots in matplotlib?
wspace and hspace
width_space and height_space
subplot_space and grid_space
padding_x and padding_y
What is the default behavior for connecting points in a matplotlib line plot?
Linear interpolation
Cubic interpolation
Step-wise connection
No connection
Which function is used to set the x-axis label in matplotlib?
set_xlabel()
set_xlim()
set_xticks()
set_title()
Which function is used to create a legend in a plot?
plt.legend()
plt.show_legend()
plt.add_legend()
plt.legend_box()
How can you customize the row and column variables in a seaborn FacetGrid?
Pass them to the row and col parameters of sns.FacetGrid()
Set them using grid.set_rows() and grid.set_cols()
Define them in the facet_vars parameter
Use the rows and columns parameters
What happens to missing values in a group key during a GroupBy operation?
They are excluded from the result
They are replaced with zeros
They are included as a separate group
An error is raised
When grouping by multiple keys, what is the type of the first element in the group tuple?
A tuple of key values
A single key value
A pandas DataFrame
A list of grouped rows
