如果列代表月份而索引是年份,如何按行读取 Pandas 的数据框中的数据并将值绘制为时间序列?

How to read the data in Pandas's dataframe row wise and plot the values as a timeseries if column represents months and index is years?

我有一个数据框,其中以月份为一列,第一列代表年份。我想绘制此数据框的时间序列,即读取每一行并绘制时间序列。 我在下面提供了一小部分数据框。 请让我知道执行此任务的任何方法。

YEAR JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC

0 1870 -0.02 -0.02 -0.01 -0.01 -0.01 -0.01 -0.01 -0.01 -0.01 -0.01 -0.01 -0.01

1 1871 -0.01 -0.01 -0.01 -0.01 -0.01 -0.01 -0.01 -0.01 -0.01 0.00 0.00 0.00

等等....

我假设您将数据存储在 pandas DataFrame 中,格式如下(每行代表一年):

df = pd.DataFrame(np.array([[1870,0.02,0.02,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01],
                             [1871,0.02,0.02,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01]]),
                   columns =  ["YEAR", "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"])

这样输出df:

     YEAR   JAN   FEB   MAR   APR   MAY  ...   JUL   AUG   SEP   OCT   NOV   DEC
0  1870.0  0.02  0.02  0.01  0.01  0.01  ...  0.01  0.01  0.01  0.01  0.01  0.01
1  1871.0  0.02  0.02  0.01  0.01  0.01  ...  0.01  0.01  0.01  0.01  0.01  0.01
[2 rows x 13 columns]

这只是一个包含重复行条目的两年样本。

您需要的是如下内容:

import matplotlib.pyplot as plt
cols = np.array(df.columns)[1:]
rows_size= df.shape[0]
x = np.empty((1, 0), str)
y = np.empty((1, 0), float)
for i in range (rows_size):
        x = np.append(x, str(int(df.iloc[i, 0]))+ "-" + cols.reshape(1,12) , axis = 1)
        y = np.append(y, np.array(df.iloc[i, 1:]).reshape(1,12), axis = 1)
x = x.reshape(-1)
y = y.reshape(-1)
plt.plot(x, y)
plt.xticks(x,x, rotation ='vertical')
plt.subplots_adjust(bottom = 0.2)
plt.show()

结果图将类似于: