迭代包含嵌套字典的多层次列表项,并在python中将其打印到csv

时间:2018-04-26 04:48:22

标签: python pandas

我想获取多层次列表,它也有子词典,并用标题写入csv。 我的json看起来像: -

"features": [
{
  "type": "Feature",
  "properties": {
    "xyz": 1,
    "abc": "pqr",
    "mmi": null
  },
  "geometry": {
    "type": "pt",
    "coordinates": [
      -118.8957,
      38.8607,
      5.3
    ]
  },
  "id": "abc101"
},

哪个应该给出以下输出, enter image description here 输出图像中显示的层次结构正是我想要得到的,但是没有找到任何适当的解决方案。

感谢您提前提供上述任何帮助。

1 个答案:

答案 0 :(得分:0)

我建议使用json_normalizeset_index所有非分层的列(列名中不是.)和.列的MultiIndexa = {"features": [ { "type": "Feature", "properties": { "xyz": 1, "abc": "pqr", "mmi": 'null' }, "geometry": { "type": "pt", "coordinates": [ -118.8957, 38.8607, 5.3 ] }, "id": "abc101" },{ "type": "Feature", "properties": { "xyz": 1, "abc": "pqr", "mmi": 'null' }, "geometry": { "type": "pt", "coordinates": [ -118.8957, 38.8607, 5.3 ] }, "id": "abc101" }]}

from pandas.io.json import json_normalize

df = json_normalize(a['features']).set_index(['id','type'])
df.columns = df.columns.str.split('.', expand=True)
print (df)
                                 geometry      properties          
                              coordinates type        abc   mmi xyz
id     type                                                        
abc101 Feature  [-118.8957, 38.8607, 5.3]   pt        pqr  null   1
       Feature  [-118.8957, 38.8607, 5.3]   pt        pqr  null   1
file

编辑:

如果想再次使用MultiIndex再次阅读df.to_csv('test.csv') df = pd.read_csv('test.csv', index_col=[0,1], header=[0,1]) print (df) geometry properties coordinates type abc mmi xyz id type abc101 Feature [-118.8957, 38.8607, 5.3] pt pqr NaN 1 Feature [-118.8957, 38.8607, 5.3] pt pqr NaN 1 ,最好不要删除第一个重复列:

from pandas.io.json import json_normalize

df = json_normalize(a['features']).set_index(['id','type'])
df.columns = df.columns.str.split('.', expand=True)

s = df.columns.get_level_values(0)
s1 = df.columns.get_level_values(1)
s0 = np.where(s.duplicated(),'',s)
df.columns = [s0, s1]

df.to_csv('test.csv')

但如果真的需要它:

path