Complete the code to create a MultiIndex from two lists.
import pandas as pd levels = [['A', 'B'], ['one', 'two']] index = pd.MultiIndex.from_tuples([1]) print(index)
The from_tuples method requires a list of tuples representing all combinations of the levels.
Complete the code to create a MultiIndex using from_product.
import pandas as pd levels = [['X', 'Y'], [1, 2]] index = pd.MultiIndex.[1](levels) print(index)
from_tuples without providing tuples.from_arrays with from_product.from_product creates a MultiIndex from the Cartesian product of input iterables.
Fix the error in creating a MultiIndex from arrays.
import pandas as pd arrays = [['a', 'a', 'b', 'b'], [1, 2, 1, 2]] index = pd.MultiIndex.from_arrays([1]) print(index)
from_arrays expects a list of arrays, so passing arrays directly is correct.
Fill both blanks to create a DataFrame with a MultiIndex from tuples and set column names.
import pandas as pd tuples = [('red', 1), ('red', 2), ('blue', 1), ('blue', 2)] index = pd.MultiIndex.[1](tuples, names=[2]) data = [10, 20, 30, 40] df = pd.DataFrame(data, index=index, columns=['value']) print(df)
from_product instead of from_tuples.names.from_tuples creates the MultiIndex from the list of tuples, and names should be a list of strings naming the index levels.
Fill all three blanks to create a MultiIndex DataFrame from product, set names, and assign columns.
import pandas as pd levels = [['A', 'B'], [1, 2]] index = pd.MultiIndex.[1](levels, names=[2]) data = [[5, 10], [15, 20], [25, 30], [35, 40]] df = pd.DataFrame(data, index=index, columns=[3]) print(df)
from_tuples instead of from_product.names or columns.from_product creates the MultiIndex from the Cartesian product of levels, names is a list naming the index levels, and columns is a list naming the DataFrame columns.