我正在尝试使用 Plotly express 在数据框中绘制数据。问题是数据以广泛的格式出现,位置(“国家”、“州”)作为行索引,而列是时间序列中的各个日期。我无法将数据转换为长格式。
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()
我可以转置数据以将日期作为行索引,但 plotly 不喜欢它。
谢谢,克里斯