有没有办法在 Altair 图表中隐藏空值?

发布于 2025-01-19 22:12:03 字数 2115 浏览 0 评论 0原文

我不希望在我的 Altair 图表上显示空值。有什么方法可以做到这一点,而不需要在绘制图表之前将它们从我的数据中删除吗?

我有一个数据框:

           d7_rent_own d8a_moving
0              Rent        NaN
1               Own         No
2               Own         No
3              Rent         No
4               Own         No
5              Rent         No
6              Rent        Yes
7               Own         No
8               Own         No
9               Own         No
10              Own         No
11              Own        Yes
12              Own         No
13             Rent        Yes
14             Rent         No
15              Own         No
16              Own         No
17              Own         No
18              Own         No
19              Own         No
20              Own         No
21             Rent        Yes
22              Own         No
23              Own         No
24              Own         No
25  No Answer Given        NaN
26             Rent        NaN
27             Rent         No
28              Own         No
29  No Answer Given        NaN
30              Own        NaN
31             Rent        Yes
32  No Answer Given         No
33             Rent        Yes
34              Own         No
35              Own        Yes
36             Rent         No
37             Rent         No
38              Own        NaN
39              Own         No
40             Rent        NaN
41             Rent        Yes
42             Rent        Yes
43             Rent         No
44              Own         No
45              Own         No
46             Rent         No
47             Rent        Yes
48             Rent         No
49             Rent         No

我正在制作一个条形图:

alt.Chart(df_ex).mark_bar().encode(
  alt.X('d7_rent_own:N', title = 'Housing Status'),
  alt.Y('count():Q', title = 'Number of Responses'),
  color = alt.Color('d8a_moving:N', legend=alt.Legend(title='Do you plan to move away?')))

但它显示如下,带有空值: 移动图表

I don't want the null values to show on my Altair chart. Is there any way to do this without removing them from my data before charting them?

I have a data frame:

           d7_rent_own d8a_moving
0              Rent        NaN
1               Own         No
2               Own         No
3              Rent         No
4               Own         No
5              Rent         No
6              Rent        Yes
7               Own         No
8               Own         No
9               Own         No
10              Own         No
11              Own        Yes
12              Own         No
13             Rent        Yes
14             Rent         No
15              Own         No
16              Own         No
17              Own         No
18              Own         No
19              Own         No
20              Own         No
21             Rent        Yes
22              Own         No
23              Own         No
24              Own         No
25  No Answer Given        NaN
26             Rent        NaN
27             Rent         No
28              Own         No
29  No Answer Given        NaN
30              Own        NaN
31             Rent        Yes
32  No Answer Given         No
33             Rent        Yes
34              Own         No
35              Own        Yes
36             Rent         No
37             Rent         No
38              Own        NaN
39              Own         No
40             Rent        NaN
41             Rent        Yes
42             Rent        Yes
43             Rent         No
44              Own         No
45              Own         No
46             Rent         No
47             Rent        Yes
48             Rent         No
49             Rent         No

And I'm making a bar chart:

alt.Chart(df_ex).mark_bar().encode(
  alt.X('d7_rent_own:N', title = 'Housing Status'),
  alt.Y('count():Q', title = 'Number of Responses'),
  color = alt.Color('d8a_moving:N', legend=alt.Legend(title='Do you plan to move away?')))

But it shows up like this, with nulls:
moving chart

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

維他命╮ 2025-01-26 22:12:03

当您将数据传递给 Altair 时,您可以在数据帧上使用 pandas dropna 删除它们。这将避免将它们从存储在变量 df_ex 中的数据帧中删除。

alt.Chart(df_ex.dropna()).mark_bar().encode(
  alt.X('d7_rent_own:N', title = 'Housing Status'),
  alt.Y('count():Q', title = 'Number of Responses'),
  color = alt.Color('d8a_moving:N', legend=alt.Legend(title='Do you plan to move away?')))

输入图片此处描述

您还可以在 Altair 中使用转换过滤器来获得相同的结果:

alt.Chart(df_ex).mark_bar().encode(
  alt.X('d7_rent_own:N', title = 'Housing Status'),
  alt.Y('count():Q', title = 'Number of Responses'),
  color = alt.Color('d8a_moving:N', legend=alt.Legend(title='Do you plan to move away?'))
).transform_filter(
    'isValid(datum.d8a_moving)'
)

You could drop these using pandas dropna on the dataframe as you are passing it to Altair. This will avoid removing them from the dataframe stored in the variable df_ex.

alt.Chart(df_ex.dropna()).mark_bar().encode(
  alt.X('d7_rent_own:N', title = 'Housing Status'),
  alt.Y('count():Q', title = 'Number of Responses'),
  color = alt.Color('d8a_moving:N', legend=alt.Legend(title='Do you plan to move away?')))

enter image description here

You could also use a transform filter in Altair for the same result:

alt.Chart(df_ex).mark_bar().encode(
  alt.X('d7_rent_own:N', title = 'Housing Status'),
  alt.Y('count():Q', title = 'Number of Responses'),
  color = alt.Color('d8a_moving:N', legend=alt.Legend(title='Do you plan to move away?'))
).transform_filter(
    'isValid(datum.d8a_moving)'
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文