简体   繁体   English

pandas改变了列的顺序

[英]pandas change the order of columns

In my project I'm using flask I get a JSON (by REST API) that has data that I should convert to a pandas Dataframe. 在我的项目中,我正在使用烧瓶,我得到一个JSON(通过REST API),其中包含我应该转换为pandas Dataframe的数据。 The JSON looks like: JSON看起来像:

{
    "entity_data":[
                  {"id": 1, "store": "a", "marker": "a"}
    ]
}

I get the JSON and extract the data: 我得到JSON并提取数据:

params = request.json
entity_data = params.pop('entity_data')

and then I convert the data into a pandas dataframe: 然后我将数据转换为pandas数据帧:

entity_ids = pd.DataFrame(entity_data)

the result looks like this: 结果如下:

   id marker store
0   1      a     a

This is not the original order of the columns. 这不是列的原始顺序。 I'd like to change the order of the columns as in the dictionary. 我想像字典中那样更改列的顺序。 help? 救命?

只需添加列名参数即可。

entity_ids = pd.DataFrame(entity_data, columns=["id","store","marker"])

Use OrderedDict for an ordered dictionary OrderedDict用于有序字典

You should not assume dictionaries are ordered. 你不应该假设订购词典。 While dictionaries are insertion ordered in Python 3.7, whether or not libraries maintain this order when reading json into a dictionary, or converting the dictionary to a Pandas dataframe, should not be assumed. 虽然字典在Python 3.7中排序的,但是将json读入字典或将字典转换为Pandas数据帧时, 无论库是否保持此顺序 ,都不应该假设。

The most reliable solution is to use collections.OrderedDict from the standard library: 最可靠的解决方案是使用标准库中的collections.OrderedDict

import json
import pandas as pd
from collections import OrderedDict

params = """{
    "entity_data":[
                  {"id": 1, "store": "a", "marker": "a"}
    ]
}"""

# replace myjson with request.json
data = json.loads(params, object_pairs_hook=OrderedDict)
entity_data = data.pop('entity_data')

df = pd.DataFrame(entity_data)

print(df)

#    id store marker
# 0   1     a      a

Assuming you have access to JSON sender, you can send the order in the JSON itself. 假设您有权访问JSON发件人,您可以在JSON本身中发送订单。

like 喜欢

`{
"order":['id','store','marker'],
"entity_data":{"id": [1,2], "store": ["a","b"],
"marker": ["a","b"]}
}

then create DataFrame with columns specified. 然后使用指定的columns创建DataFrame。 as said by Chiheb.K. 正如Chiheb.K所说。

import pandas as pd
params = request.json
entity_data = params.pop('entity_data')
order = params.pop('order')
entity_df=pd.DataFrame(data,columns=order)

if you cannot explicitly specify the order in the JSON. 如果您无法在JSON中明确指定顺序。 see this answer to specify object_pairs_hook in JSONDecoder to get an OrderedDict and then create the DataFrame 看到这个答案在JSONDecoder指定object_pairs_hook以获取OrderedDict ,然后创建DataFrame

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM