Headers as years in multiple columns. How to setup a line chart?

Hey @mocoroh ,

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()

creates:
newplot(49)

1 Like