Search code examples
pythonpandasplotly-express

How do I reshape this data to use Plotly


I'm trying to use Plotly express to plot data in a data frame. The problem is that the data comes in a wide format with locations ('Country', 'State') as row indexes and the columns are individual dates in a time series. I'm having trouble getting the data into a long format.

import plotly.graph_objects as go
import plotly.express as px
import numpy as np
import pandas as pd


# Create the data frame
nam = ['Country','State']
tups = [
    ('US', 'Westchester County, NY'),
    ('US', 'King County, WA'),
    ('US', 'Diamond Princess'),
    ('US', 'Santa Clara County, CA'),
    ('US', 'Snohomish County, WA'),
    ('China','Hubei'),
    ('China', 'Anhui'),
    ('China', 'Heibei'),
    ('China', 'Fujian'),
    ('China', 'Beiijing')
]

cols = [
    '3/4/20', '3/5/20', '3/6/20', 
    '3/7/20', '3/8/20', '3/9/20', 
    '3/10/20', '3/11/20', '3/12/20', 
    '3/13/20'
]

a = np.random.random([10])+2
b = np.arange(0,10)
c = np.outer(a,b)

 
idx = pd.MultiIndex.from_tuples(tups)
samp = pd.DataFrame(data = c,columns = cols,index=idx)                    
samp.index.names=nam       
        
samp
# fig = px.line( x='Date', y='values')
# fig.show()

I can transpose the data to get the dates as a row index, but plotly doesn't like it.

Thanks, Chris


Solution

  • this is a job that DataFrame.melt() can handle!

    samp.reset_index().melt(id_vars=['Country','State'])