Col0 Col1 Col2
2015 1 4
2016 2 3
形式的数据是浮点数据文件,我用numpty
loadtext
做出ndarray
。但是,我需要跳过标签行和列以获得数据数组。在阅读标签时,我怎样才能使ndarray
不在数据中?
import numpy as np
import matplotlib.pyplot as plt
data = np.loadtxt("data.csv", skiprows=1)
# I need to skip the first row in reading the data but still get the labels.
x= data[:,0]
a= data[:,1]
b= data[:,2]
plt.xlabel(COL0) # Reading the COL0 value from the file.
plt.ylabel(COL1) # Reading the COL1 value from the file.
plt.plot(x,a)
注:标签(列标题)是在脚本未知。该脚本应该是通用的,以便与任何具有相同结构的输入文件一起工作。
通常人们使用大熊猫这样的任务。 'df = pandas.read_csv()'会给你一个带有命名列的数据框,这样你就可以在'df.columns'中访问列名。 – ImportanceOfBeingErnest