我有以下格式的数据框:
ts
label
A [stamp1, stamp2, ...]
B [stamp1, stamp2, ...]
C
D
我想将一个时间序列绘制为每个标签的行。我怎么能在熊猫中轻松做到?
答案 0 :(得分:1)
您可以尝试:
import pandas as pd
import numpy as np
df = pd.DataFrame({"ts": pd.Series([[pd.Timestamp('2014-04-08 00:00:00'), pd.Timestamp('2014-04-09 00:00:00'), pd.Timestamp('2014-04-10 00:00:00')],
[pd.Timestamp('2014-05-08 00:00:00'), pd.Timestamp('2014-05-09 00:00:00'), pd.Timestamp('2014-05-10 00:00:00')],
[pd.Timestamp('2014-06-08 00:00:00'), pd.Timestamp('2014-06-09 00:00:00'), pd.Timestamp('2014-06-10 00:00:00')],
[pd.Timestamp('2014-07-08 00:00:00'), pd.Timestamp('2014-07-09 00:00:00'), pd.Timestamp('2014-07-10 00:00:00')]],
index=[ 'A', 'B', 'C', 'D'])})
print df
ts
A [2014-04-08 00:00:00, 2014-04-09 00:00:00, 201...
B [2014-05-08 00:00:00, 2014-05-09 00:00:00, 201...
C [2014-06-08 00:00:00, 2014-06-09 00:00:00, 201...
D [2014-07-08 00:00:00, 2014-07-09 00:00:00, 201...
#convert lists to columns
df = pd.DataFrame([ x for x in df['ts']], index=df.index)
print df
0 1 2
A 2014-04-08 2014-04-09 2014-04-10
B 2014-05-08 2014-05-09 2014-05-10
C 2014-06-08 2014-06-09 2014-06-10
D 2014-07-08 2014-07-09 2014-07-10
#cast datetime to integer
df = df.astype(np.int64)
print df
0 1 2
A 1396915200000000000 1397001600000000000 1397088000000000000
B 1399507200000000000 1399593600000000000 1399680000000000000
C 1402185600000000000 1402272000000000000 1402358400000000000
D 1404777600000000000 1404864000000000000 1404950400000000000
#transpose and plot df
df.T.plot()
df.plot()