Consider the following pandas DataFrame and code that sets MultiIndex columns. What will be the output of print(df.columns)?
import pandas as pd cols = [('A', 'foo'), ('A', 'bar'), ('B', 'baz')] df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=pd.MultiIndex.from_tuples(cols)) print(df.columns)
Look at how pd.MultiIndex.from_tuples creates a MultiIndex from a list of tuples.
The columns are set as a MultiIndex with levels 'A' and 'B' as the first level and 'foo', 'bar', 'baz' as the second level. So the output shows a MultiIndex with tuples as column labels.
Given the code below, how many columns does the DataFrame df have?
import pandas as pd arrays = [['one', 'one', 'two', 'two'], ['A', 'B', 'A', 'B']] cols = pd.MultiIndex.from_arrays(arrays, names=['level_1', 'level_2']) df = pd.DataFrame([[1, 2, 3, 4], [5, 6, 7, 8]], columns=cols) print(len(df.columns))
Count the number of tuples in the MultiIndex columns.
The MultiIndex columns have 4 tuples: ('one', 'A'), ('one', 'B'), ('two', 'A'), ('two', 'B'). So the DataFrame has 4 columns.
What error will this code raise?
import pandas as pd
cols = [('A', 'foo'), ('B',)]
df = pd.DataFrame([[1, 2], [3, 4]], columns=pd.MultiIndex.from_tuples(cols))Check if all tuples have the same length when creating MultiIndex.
MultiIndex requires all tuples to have the same length. Here, one tuple has length 2, the other length 1, so it raises a ValueError.
Which code snippet will create a DataFrame with MultiIndex columns and display it correctly?
Check how MultiIndex is created and that arrays have matching lengths.
Option C correctly creates a MultiIndex from tuples of equal length and assigns it as columns. Option C uses a list of tuples but not as MultiIndex, so columns are treated as normal tuples, causing error. Option C uses pd.Index with tuples, which does not create MultiIndex. Option C uses from_arrays with arrays of different lengths, causing error.
Given the DataFrame below, what is the value at the column ('B', 'two') for the second row (index 1)?
import pandas as pd arrays = [['A', 'A', 'B', 'B'], ['one', 'two', 'one', 'two']] cols = pd.MultiIndex.from_arrays(arrays) data = [[10, 20, 30, 40], [50, 60, 70, 80]] df = pd.DataFrame(data, columns=cols) value = df.loc[1, ('B', 'two')] print(value)
Look at the column labels and the row index carefully.
The column ('B', 'two') corresponds to the fourth column. For row index 1, the value is 80.