有没有办法在 Altair 图表中隐藏空值?
Is there a way to hide nulls in an Altair chart?
我不想在我的 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?')))
但它是这样显示的,带有空值:
您可以在将数据传递给 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)'
)
我不想在我的 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?')))
但它是这样显示的,带有空值:
您可以在将数据传递给 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)'
)