How do you create a DataFrame in pandas from a dictionary?
A
pd.DataFrame.from_dict() B
pd.DataFrame() C
pd.read_dict() D
pd.dict_to_df()
Analysis & Theory
You can pass a dictionary directly to pd.DataFrame() to create a DataFrame.
Which attribute returns the column labels of a DataFrame?
A
df.columns B
df.labels C
df.colnames D
df.names
Analysis & Theory
`df.columns` gives the column names (labels) of the DataFrame.
How can you access the first 5 rows of a DataFrame `df`?
A
df.first(5) B
df[:5] C
df.head() D
df.head(5)
Analysis & Theory
`df.head(5)` is the correct method. By default, `head()` also returns 5 rows.
Which method returns the data types of each column in a DataFrame?
A
df.types() B
df.dtypes C
df.coltypes D
df.get_types()
Analysis & Theory
`df.dtypes` returns the data types of all columns.
What does `df.iloc[2]` return?
A
Row with index label 2 B
Row at position 2 (third row) C
Column with index 2 D
Cell at row 2 and all columns
Analysis & Theory
`iloc` accesses rows/columns by integer position. So `df.iloc[2]` gives the third row.
How do you select multiple columns from a DataFrame?
A
df[['col1', 'col2']] B
df['col1', 'col2'] C
df.select('col1', 'col2') D
df.col1.col2
Analysis & Theory
To select multiple columns, use double square brackets with a list of column names.
What will `df.loc[1:3]` return?
A
Rows at positions 1 to 3 (excluding 3) B
Rows with labels 1, 2, 3 C
Rows with index 1 and 3 D
Only row 3
Analysis & Theory
`loc` is label-based and includes the stop index. So rows with labels 1, 2, 3 are returned.
How can you add a new column to a DataFrame?
A
df.add_column('new', values) B
df.insert('new', values) C
df['new'] = values D
df.new(values)
Analysis & Theory
You can add a new column simply by assigning values: `df['new'] = values`.
What does `df.T` do to the DataFrame?
A
Removes rows B
Transposes the DataFrame C
Deletes columns D
Creates a temporary view
Analysis & Theory
`df.T` transposes the DataFrame — rows become columns and vice versa.
Which method returns the number of non-null values in each column?
A
df.count() B
df.value_counts() C
df.notnull() D
df.shape
Analysis & Theory
`df.count()` returns the count of non-null values for each column.