首页 > 解决方案 > PowerBI - Pandas isnull().sum() 索引

问题描述

我在 PowerBI 中运行以下 Python 脚本

# 'dataset' holds the input data for this script
import pandas as pd
import numpy as np

df=pd.DataFrame(np.sum(dataset.isnull(), axis=0),columns=['Null_Values'])
df['Items']=dataset.isnull().index

几点注意事项:

空值

0

0

5

0

4

所以你只能猜测 5 和 4 指的是哪一列。在 Power BI 之外,dataset.isnull().sum()通常会产生

            Null Values
Column A      0
Coulmn B      0
Column C      5
Column D      6 

如果我包含代码片段的最后一行,我会收到一个错误 ValueError: Length of values does not match length of index

我只想再添加一列来描述原始列名(列 A、B、C 等)

原始数据集:

Department,OfficeCode,Sales,Target,Staffing,Dotation
South,23,80,100,0.8,10
South,24,67,100,0.78,15
North,33,111,120,0.98,10
South,25,87,100,,15
West,43,45,50,0.98,10
East,53,65,80,0.89,15
North,34,110,120,,10
North,35,112,120,0.96,15
North,36,,120,0.94,10
North,37,98,120,0.85,15
South,26,77,100,0.7,10
East,54,64,80,0.8,15
East,55,,80,,11
West,44,44,50,,14
East,56,,80,0.79,15
West,44,,50,0.9,16
West,45,34,50,0.89,13
South,27,,100,,14

编辑:好的,我已经通过更改最后一行来解决它:

df['Items']=dataset.isnull().index

df['Items']=df.index

它所做的是复制索引列,以便可以在 PowerBi 查询编辑器表中看到它。

也许,一个后续问题是如何让 PowerBi 将索引列视为任何其他列并显示它?

标签: pythonpandaspowerbi

解决方案


推荐阅读