In pandas the inner most index must label each row.
df = df.set_index('Customer ID', append=True).swaplevel(0,1)
Output:
Category VALUE
Customer ID
HETO90 0 Baby Sets 1000.0
1 Girls Dresses 5356.0
2 Girls Jumpers 2822.0
3 Girls Top 13398.0
4 Shorts 7590.0
Answer from Scott Boston on Stack Overflowpython - Pandas: Reading Excel with merged cells - Stack Overflow
Merge cells with st.dataframe or st.table
Excel Merge and Center using python pandas
How do I merge cells in Pandas + Python? - Stack Overflow
Videos
You could use the Series.fillna method to forword-fill in the NaN values:
df.index = pd.Series(df.index).fillna(method='ffill')
For example,
In [42]: df
Out[42]:
Sample CD4 CD8
Day 1 8311 17.30 6.44
NaN 8312 13.60 3.50
NaN 8321 19.80 5.88
NaN 8322 13.50 4.09
Day 2 8311 16.00 4.92
NaN 8312 5.67 2.28
NaN 8321 13.00 4.34
NaN 8322 10.60 1.95
[8 rows x 3 columns]
In [43]: df.index = pd.Series(df.index).fillna(method='ffill')
In [44]: df
Out[44]:
Sample CD4 CD8
Day 1 8311 17.30 6.44
Day 1 8312 13.60 3.50
Day 1 8321 19.80 5.88
Day 1 8322 13.50 4.09
Day 2 8311 16.00 4.92
Day 2 8312 5.67 2.28
Day 2 8321 13.00 4.34
Day 2 8322 10.60 1.95
[8 rows x 3 columns]
To casually come back 8 years later, pandas.read_excel() can solve this internally for you with the index_col parameter.
df = pd.read_excel('path_to_file.xlsx', index_col=[0])
Passing index_col as a list will cause pandas to look for a MultiIndex. In the case where there is a list of length one, pandas creates a regular Index filling in the data.
I have a data frame with country names and capitals and a data frame with missing capitals. I want to merge them so that the data frame with missing capitals replaces the 'Not found' cells in the main data frame.
Here are the two data frames for reference:
```capitals_df```
| CAPITAL | COUNTRY | |
|---|---|---|
| 0 | Kabul | Afghanistan |
| 1 | Tirana | Albania |
| 2 | Algiers | Algeria |
| 3 | Pago Pago | American Samoa |
| 4 | Not found | Andorra |
240 rows x 6 columns
```missing_capitals_df```
| 0 | CAPITAL |
|---|---|
| 4 | Andorra la Vella |
| 7 | Does not exist |
| 15 | Nassau |
| 30 | Road Town |
| 45 | West Island |
I tried merging the two data frames but I end up with two separate rows `CAPITAL_x` and `CAPITAL_Y`. Here is the code:
pd.merge(capitals_df, missing_capitals_df, left_index=True, right_index=True, how='left')
How to ensure that the data frame replaces specific cells based on indices, instead of creating a separate column?
