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?')))
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?')))
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)'
)