Missing Values In Pandas Column Multiindex
I am reading with pandas excel sheets like this one: using df = pd.read_excel('./question.xlsx', sheet_name = None, header = [0,1]) which results in multiindex dataframe with mul
Solution 1:
Assuming that you want to have empty strings instead of repeating the first label, you can read the 2 lines and build the MultiIndex directly:
df1 = pd.read_excel('./question.xlsx', header = None, nrows=2).fillna('')
index = pd.MultiIndex.from_arrays(df1.values)
it gives:
MultiIndex([('Title', '#'),
( '', 'Price'),
( '', 'Quantity')],
)
By the way, if you wanted a different label for empty fields, you can just use it as the parameter for fillna
.
Then, you just read the remaining data, and set the index by hand:
df1 = pd.read_excel('./question.xlsx', header = None, skiprows=2)
df1.columns = index
Post a Comment for "Missing Values In Pandas Column Multiindex"