python pd.read_excel读取多级目录exel表格xlsx数据,进行类别一对一填充和映射

如下

在这里插入图片描述

代码

借助当前列上一个类别数据,以及下一级目录的数据,得出综合判断后进行填充

    def df2list(self, col, col_1):
        """
        pass
        """
        tmp = ''
        for index, txt in enumerate(col):
            if txt != '':
                tmp = txt
            elif col_1[index] != '':
                col[index] = tmp
        return col
df = pd.read_excel(self.con_target_municipal, engine='openpyxl',
                               usecols=['一级', '二级', '三级', '四级', '五级', '办理等级'],
                               na_values=' ', keep_default_na=False)
            col_5 = df['五级'].values.tolist()
            col_4 = self.df2list(df['四级'].values.tolist(), col_5)
            col_3 = self.df2list(df['三级'].values.tolist(), col_4)
            col_2 = self.df2list(df['二级'].values.tolist(), col_3)
            col_1 = self.df2list(df['一级'].values.tolist(), col_2)

猜你喜欢

转载自blog.csdn.net/qq_15821487/article/details/124988411