【发布时间】:2016-08-29 22:24:02
【问题描述】:
在 python 中使用 numpy genfromtxt,我希望能够将列标题作为给定数据的键。我尝试了以下方法,但无法获取相应数据的列名。
column = np.genfromtxt(pathToFile,dtype=str,delimiter=',',usecols=(0))
columnData = np.genfromtxt(pathToFile,dtype=str,delimiter=',')
data = dict(zip(column,columnData.tolist()))
下面是数据文件
header0,header1,header2
mydate,3.4,2.0
nextdate,4,6
afterthat,7,8
目前,它将数据显示为
{
"mydate": [
"mydate",
"3.4",
"2.0"
],
"nextdate": [
"nextdate",
"4",
"6"
],
"afterthat": [
"afterthat",
"7",
"8"
]
}
我想采用这种格式
{
"mydate": {
"header1":"3.4",
"header2":"2.0"
},
"nextdate": {
"header1":"4",
"header2":"6"
},
"afterthat": {
"header1":"7",
"header2": "8"
}
}
有什么建议吗?
【问题讨论】:
-
你考虑过pandas吗?
标签: python numpy genfromtxt