简体   繁体   English

熊猫读取excel文件并填充缺失值

[英]Pandas read excel file and fill missing values

I have imported this excel file into Pandas as follows:我已将这个excel 文件导入到 Pandas 中,如下所示:

xlsnist = pd.ExcelFile(path+'framework-for-improving-critical-infrastructure-cybersecurity-core.xlsx')

df3 = pd.read_excel(xlsnist, "CSF Core")

The screenshot below shows that this file has merged cells.下面的屏幕截图显示此文件已合并单元格。 I want to fill the empty rows with the relevant values for Function, Category, Subcategory.我想用函数、类别、子类别的相关值填充空行。 For example the NaN cells of Function should have "IDENTIFY (ID)" until it changes to "PROTECT (PR)" at row 82. I want to do this for all columns so there are no "NaN" values but I'm not sure how to do this.例如,函数的 NaN 单元格应该有“IDENTIFY(ID)”,直到它在第 82 行变为“PROTECT(PR)”。我想对所有列都这样做,所以没有“NaN”值,但我不是确定如何做到这一点。

在此处输入图片说明

在此处输入图片说明

You can try:你可以试试:

import pandas as pd
file = 'framework-for-improving-critical-infrastructure-cybersecurity-core.xlsx'
df = pd.read_excel(file)
df.ffill()

Result:结果:

在此处输入图片说明

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

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