有没有办法在 Altair 图表中隐藏空值?
我不希望在我的 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?')))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您将数据传递给 Altair 时,您可以在数据帧上使用 pandas
dropna
删除它们。这将避免将它们从存储在变量 df_ex 中的数据帧中删除。您还可以在 Altair 中使用转换过滤器来获得相同的结果:
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 variabledf_ex
.You could also use a transform filter in Altair for the same result: