【发布时间】:2018-11-05 16:03:38
【问题描述】:
我有一个*.csv 文件,其中包含第一列格式为"YYYY-MM" 的日期、第二列上的字母和两列数据的数据。
它看起来像这样:
Date inflation rate CPI-Value LIBOR-Rate
2003-09 inflation rate 80.172 0.81
2003-10 inflation rate 80.132 0.88
2003-11 inflation rate 80.264 0.69
2003-12 inflation rate 80.430 0.75
2004-01 inflation rate 81.163 0.75
2004-02 inflation rate 81.244 0.75
2004-03 inflation rate 81.344 0.75
2004-04 inflation rate 81.436 0.75
2004-05 inflation rate 81.501 0.75
2004-06 inflation rate 81.355 0.81
2004-07 inflation rate 81.494 1.06
2004-08 inflation rate 81.426 1.31
2004-09 inflation rate 81.771 1.44
2004-10 inflation rate 81.757 1.38
2004-11 inflation rate 81.866 1.38
2004-12 inflation rate 81.790 1.44
2005-01 inflation rate 81.994 1.75
2005-02 inflation rate 82.062 1.94
2005-03 inflation rate 82.210 2.13
2005-04 inflation rate 82.219 2.13
2005-05 inflation rate 82.165 2.06
我想绘制一个以日期为 x 轴的折线图,以及一个包含 CPI 和 LIBOR 值的图。
我尝试过使用
x, y = np.genfromtxt(CPI_df, usecols=(0, 2), unpack=True, delimiter=',')
plt.plot(x, y, 'ro--')
plt.show()
但是有一个值错误,指出某些行只有一列而不是两列。但是,我已经检查了csv文件,没有丢失数据。
感谢我能得到的任何帮助,谢谢!
【问题讨论】:
-
您使用
delimiter=',',但您引用的文件中似乎没有逗号 -
这是一个 csv 文件。那么这是否意味着我不需要放置分隔符?
-
如果你显示的三行真的是你文件的前三行,那很明显没有逗号。但是我不确定在这种情况下使用什么其他分隔符,因为它似乎在单元格内也有空格。
-
如果有帮助,我正在使用 jupyter notebook 运行代码。不知道有没有区别。
-
不,在这种情况下没关系。如果您在编辑器中打开文件,并将前十行逐字复制到您的问题中,将会有帮助。
标签: python-3.x csv matplotlib