在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"
  }
}


有什么建议么?

最佳答案

通过您的样本文件和genfromtxt调用,我得到2个数组:

In [89]: column
Out[89]:
array(['header0', 'mydate', 'nextdate', 'afterthat'],
      dtype='<U9')
In [90]: columnData
Out[90]:
array([['header0', 'header1', 'header2'],
       ['mydate', '3.4', '2.0'],
       ['nextdate', '4', '6'],
       ['afterthat', '7', '8']],
      dtype='<U9')


拉出columnData的第一行

In [91]: headers=columnData[0,:]
In [92]: headers
Out[92]:
array(['header0', 'header1', 'header2'],
      dtype='<U9')


现在构造一个字典字典(我不需要单独的column数组):

In [94]: {row[0]: {h:v for h,v in zip(headers, row)} for row in columnData[1:]}
Out[94]:
{'afterthat': {'header0': 'afterthat', 'header1': '7', 'header2': '8'},
 'mydate': {'header0': 'mydate', 'header1': '3.4', 'header2': '2.0'},
 'nextdate': {'header0': 'nextdate', 'header1': '4', 'header2': '6'}}


完善一下:

In [95]: {row[0]: {h:v for h,v in zip(headers[1:], row[1:])} for row in columnData[1:]}
Out[95]:
{'afterthat': {'header1': '7', 'header2': '8'},
 'mydate': {'header1': '3.4', 'header2': '2.0'},
 'nextdate': {'header1': '4', 'header2': '6'}}


我喜欢字典理解!

您的清单字典版本:

In [100]: {row[0]:row[1:] for row in columnData[1:].tolist()}
Out[100]: {'afterthat': ['7', '8'], 'mydate': ['3.4', '2.0'], 'nextdate': ['4', '6']}

08-25 01:31