Hi my gorgeous friend on the Internet, I have a question about how to plot a Chart in Altair.
I have been struggling with this error since yesterday and still haven't plotted successfully.
I first imported some modules and data,
import pandas as pd
import altair as alt
import numpy as np
url = "https://github.com/byuidatascience/data4missing/raw/master/data-raw/flights_missing/flights_missing.json"
flights = pd.read_json(url)
flights.head()
Then, I used groupby() to add all num_of_total_flights together for each airport_code.
ttl = flights.groupby('airport_code')['num_of_flights_total'].sum()
ttl
After I checked ttl was fine, I wanted to plot the chart.
alt.Chart(ttl).mark_bar().encode(
y="airport_code",
x="num_of_flights_total",
)
Then I got this error message: num_of_flights_total encoding field is specified without a type; the type cannot be automatically inferred because the data is not specified as a pandas.DataFrame.
Does anyone know how to solve this?? Thanks for your valuable time in advance.
Hello there charismatic stranger, I have an answer to your question. The error message is misleading in this case (and will likely be improved in the nearish future). Because .groupby().sum()
returns as series, you need to reset the index to get airport_code
as a column name:
ttl = flights.groupby('airport_code')['num_of_flights_total'].sum().reset_index()
alt.Chart(ttl).mark_bar().encode(
y="airport_code",
x="num_of_flights_total",
)