I need to generate a line chart with some items from the INDICADOR column to show values from the columns 2008 to 2021. Usually the years are in a sole column, so that would be easy. I have started with this:
import pandas as pd
import plotly.offline as pyo
import plotly.graph_objs as go
linhas_imp = [0,3,14,60] # here I am picking the rows I want
df = pd.read_csv(âCensoEscolarSinopseREC.csvâ, sep = â;â, skiprows = lambda x: x not in linhas_imp) #is this correct?
df = df.loc[:, ~df.columns.isin([âNĂvelâ,âUnidadeâ])] #excluding the columns I do not need to use
And now I am quite confused on setting up the:
data = [go.Scatter(x=???,
y=???,
mode=âlinesâ,
name=???
)]
pyo.plot(data)
Could anyone give me some advice? I have tried to find exhaustively on Google but did not find any similar case of CSV layout. THANKS!!!
Hi AIMPED!
Thank you very much for the prompt response. It worked great. I would be very glad if you could show how to disclosure 3 or 4 lines (e.g. including the rows CRECHE, MUNICIPAL, ESTADUAL) and using plotly.offline plotly.graph_objs, since these have been the packages I am studying at the moment (because I am going towards Dash).
there are a lot of different ways to approach this. My fisrt post shows the quick & dirty version. I prefer cleaning the dataframe bofore plotting figures. This is the major part of the following code:
import pandas as pd
import plotly.graph_objects as go
# read csv file (I renamed the two items "Municipal" in the column "Indicador")
df = pd.read_csv('extract.txt', delimiter=';')
# delete unwanted columns
cleaned = df.drop(['NĂvel', 'Unidade'], axis=1)
# transpose (switch columns/rows)
cleaned = cleaned.transpose(copy=True)
#reset index
cleaned.reset_index(inplace=True)
# use first row of DataFrame as header
cleaned.columns = cleaned.iloc[0]
# delete first row
cleaned = cleaned.iloc[1:]
# covert types
cleaned = cleaned.astype(float)
cleaned.Indicador = pd.to_datetime(cleaned.Indicador, format='%Y')
# now the easy part --> create the figure
fig = go.Figure()
# add each column as trace
for column in ['Ensino infantil', 'Creche', 'Municipal_1']:
fig.add_scatter(x=cleaned['Indicador'], y=cleaned[column], mode='markers+lines', name=column)
# show figure
fig.show()