简体   繁体   English

Python Dataframe fillna 左列有值

[英]Python Dataframe fillna with value on left column

I have an excel spreadsheet where there are merged cells.我有一个 excel 电子表格,其中包含合并的单元格。

在此处输入图像描述

I would like to build a dictionary of Product_ID - Category - Country.我想建立一个 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.但为此,我相信,我需要获得 Python 才能读取具有水平合并单元格的 excel 文件。

import pandas as pd

excel_sheet = pd.read_excel(r'C:\Users\myusername\Documents\Product_Sales_Database.xlsx, 'Product IDs')

However the returned dataframe is this:但是返回的 dataframe 是这样的:

在此处输入图像描述

My question is, how can I fill the nan values in the dataframe, with the values on the left column?我的问题是,如何使用左列中的值填充 dataframe 中的 nan 值?

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:我明白,问题超过 2 岁,最佳答案是正确的,但如果你想填充整个框架的 NaN,你可以使用:

df.T.ffill().T 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM