Pandas dict to dataframe - columns out of order?

前端 未结 2 672
死守一世寂寞
死守一世寂寞 2021-01-16 06:18

I did a search but didn\'t see any results pertaining to this specific question. I have a Python dict, and am converting my dict to a pandas dataframe:

panda         


        
2条回答
  •  Happy的楠姐
    2021-01-16 07:08

    Python dictionary is an unordered structure, and the key order you get when printing it (or looping over its keys) is arbitrary.

    In this case, you would need to explicitly specify the order of columns in the DataFrame with,

    pandas.DataFrame(data=data_dict, columns=columns_order)
    

    where column_order is a list of column names in the order.

提交回复
热议问题