mwhy
mwhy

Reputation: 9

How to create a plotly line plot from a pandas DataFrame with lists

I have a pandas DataFrame created from a dict and would like to create a plotly line plot from it. The DataFrame contains lists however and looks like this:

                    a  ...                   L
0                 a_0  ...  [L_00,       L_01]
1                 a_1  ...  [L_10,       L_11]
2                 a_2  ...  [L_20,       L_21]
3                 a_3  ...  [L_30,       L_31]

My plot should be the values of L_i0 plotted against a_i but I can only create the plot giving the name of a column like this:

fig = px.line(dataframe, x='a', y='L')

I know that I can access the values like this ['L'][i][0] and then iterate over i but is it possible to tell plotly to take only the first values of the list L?

Sample dataframe

df = pd.DataFrame({'a':[1,2,3],
                   'L':[[10,11,12], [20,21,22], [30,31,21]]})

Upvotes: 1

Views: 1996

Answers (3)

mwhy
mwhy

Reputation: 9

I had read the documentation wrong and found a way to do what I want to do:

fig = px.line(dataframe, x='a',
              y=[tupel[0] for tupel in dataframe['L']])

this accesses the first element in the lists which are tupels contained in L.

Upvotes: -1

amal lal
amal lal

Reputation: 11

import the libraries

import matplotlib.pyplot as plt
import pandas as pd

creating dataframe using pandas

data = [['DS', 'Linked_list', 10], ['DS', 'Stack', 9], ['DS', 'Queue', 7], 
        ['Algo', 'Greedy', 8], ['Algo', 'DP', 6], ['Algo', 'BackTrack', 5], ]  
df = pd.DataFrame(data, columns = ['Category', 'Name', 'Marks'])  

ploting the numeric columns

plt.plot(df["Marks"])
plt.xlabel

Upvotes: 1

vestland
vestland

Reputation: 61234

I can't see why, but if it is in fact correct that you

[...] can only create the plot giving the name of a column like this

Then you should really consider making sure that df['L'] does in fact only contain the values you'd like to plot, and not lists with a bunch of other stuff. But you may very well have good reason to organize your data exactly the way you have, so I suspect your question is in fact an XY problem.

But if you're not bound to using plotly.express, then plotly.graph_objects can do exactly what you seem to aim for here using, among other things, df.iterrows(). I'm assuming that this datastructure resembles your real world scenario:

   a        L
0  1  [10, 11, 12]
1  2  [20, 21, 22]
2  3  [30, 31, 21]

Code:

import pandas as pd
import plotly.graph_objects as go

df = pd.DataFrame({'a':[1,2,3],
                   'L':[[10,11,12], [20,21,22], [30,31,21]]})

aVals = []
LVals = []
for i, row in df.iterrows():
    aVals.append(row[0])
    LVals.append(row[1][0])
    
fig = go.Figure(go.Scatter(x=aVals, y=LVals))
fig.show()

Plot:

enter image description here

Upvotes: 2

Related Questions