Python Dataframe fillna with value on left column
Question:
I have an excel spreadsheet where there are merged cells.
I would like to build a dictionary of Product_ID – Category – Country.
But for that I need to get, I believe, Python to be able to read an excel file with horizontally merged cells.
import pandas as pd
excel_sheet = pd.read_excel(r'C:UsersmyusernameDocumentsProduct_Sales_Database.xlsx, 'Product IDs')
However the returned dataframe is this:
My question is, how can I fill the nan values in the dataframe, with the values on the left column?
Thank you!
Answers:
This should do:
df.iloc[0] = df.iloc[0].ffill()
I understand, that the question is more than 2 yo, and the best answer is correct, but if you want to fill NaNs of the whole Frame, you can use:
df.T.ffill().T
I have an excel spreadsheet where there are merged cells.
I would like to build a dictionary of Product_ID – Category – Country.
But for that I need to get, I believe, Python to be able to read an excel file with horizontally merged cells.
import pandas as pd
excel_sheet = pd.read_excel(r'C:UsersmyusernameDocumentsProduct_Sales_Database.xlsx, 'Product IDs')
However the returned dataframe is this:
My question is, how can I fill the nan values in the dataframe, with the values on the left column?
Thank you!
This should do:
df.iloc[0] = df.iloc[0].ffill()
I understand, that the question is more than 2 yo, and the best answer is correct, but if you want to fill NaNs of the whole Frame, you can use:
df.T.ffill().T