1

这个 Python def 提供了一个列名,但 Altair 代码似乎没有使用它

def gapsindata( name_of_column ):
    print(outer_join_df )
    
    
     
    print("'" + name_of_column  + ":O'")
    print("'" + name_of_column  + ":N'")
    
    bars =  alt.Chart(outer_join_df).mark_bar().encode(
    alt.Y('PercentMissing:Q'),
    x= "'" + name_of_column  + ":O'",
    color= "'" + name_of_column  + ":N'",
    )

    text = bars.mark_text(
    align='left',
    baseline='middle',
    dx=1  # Nudges text to right so it doesn't appear on top of the bar
    ).encode(
    text='PercentMissing:Q'
    )

    (bars + text).properties(height=200).facet(column= "'" + name_of_column  + ":N'")
 
    
    return;


gapsindata('DfService')

在使用通过 Python def 参数输入的列名时,我无法显示 Altair 图。这是输出:

Outer Join DF
   DfService|       FieldName  |MissingItemCnt  |ItemCnt  |PercentMissing
0         No|    Model Number  |             0  |     53  |      0.000000
1         No|        SKU type  |             8  |     53  |     15.094340
2         No|  Equipment Type  |             1  |     53  |      1.886792
3        Yes|    Model Number  |             0  |    204  |      0.000000
4        Yes|        SKU type  |            52  |    204  |     25.490196
5        Yes|  Equipment Type  |             1  |    204  |      0.490196
'DfService:O'
'DfService:N'

提前致谢

4

1 回答 1

1

您的函数正在返回None,因此您的笔记本None在函数调用后显示。

它类似于此功能:

def f(x):
  2 * x
  return None

调用函数时你不会看到2 * x结果,因为函数没有对它做任何事情。

如果要显示图表,一个好的方法是返回图表:

def gapsindata( name_of_column ):
    ...

    return (bars + text).properties(height=200).facet(column= "'" + name_of_column  + ":N'")

gapsindata('DfService')
于 2021-05-04T17:16:35.283 回答