在python中从numpy genfromtxt获取列名



在python中使用numpy genfromtext,我希望能够获得列标头作为给定数据的关键。我尝试了以下操作,但无法获得相应数据的列名。

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模块:

In [94]: fn = r'D:temp.dataz.csv'

读取CSV到数据帧:

In [95]: df = pd.read_csv(fn)
In [96]: df
Out[96]:
     header0  header1  header2
0     mydate      3.4      2.0
1   nextdate      4.0      6.0
2  afterthat      7.0      8.0

get desired dict:

In [97]: df.set_index('header0').to_dict('index')
Out[97]:
{'afterthat': {'header1': 7.0, 'header2': 8.0},
 'mydate': {'header1': 3.3999999999999999, 'header2': 2.0},
 'nextdate': {'header1': 4.0, 'header2': 6.0}}

或JSON字符串:

In [107]: df.set_index('header0').to_json(orient='index')
Out[107]: '{"mydate":{"header1":3.4,"header2":2.0},"nextdate":{"header1":4.0,"header2":6.0},"afterthat":{"header1":7.0,"header2":8.0}}'

使用示例文件和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']}

相关内容

  • 没有找到相关文章

最新更新