繁体   English   中英

如果列表示月份并且索引是年份,如何按行读取 Pandas 的 dataframe 和 plot 中的值作为时间序列?

[英]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?

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

年份 1 月 2 月 3 月 4 月 5 月 6 月 7 月 8 月 9 月 10 月 11 月 12 月

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()

生成的 plot 将类似于: (图片)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM