pandas.concat中的列顺序 [英] Column order in pandas.concat

查看:577
本文介绍了pandas.concat中的列顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按如下操作:

data1 = pd.DataFrame({ 'b' : [1, 1, 1], 'a' : [2, 2, 2]})
data2 = pd.DataFrame({ 'b' : [1, 1, 1], 'a' : [2, 2, 2]})
frames = [data1, data2]
data = pd.concat(frames)
data


   a    b
0   2   1
1   2   1
2   2   1
0   2   1
1   2   1
2   2   1

数据列顺序为字母顺序。为什么会这样呢?
以及如何保持原始顺序?

The data column order is in alphabet order. Why is it so? and how to keep the original order?

推荐答案

您正在用词典创建DataFrame。字典是无序的,这意味着键没有特定的顺序。因此

You are creating DataFrames out of dictionaries. Dictionaries are a unordered which means the keys do not have a specific order. So

d1 = {'key_a': 'val_a', 'key_b': 'val_b'}

d2 = {'key_b': 'val_b', 'key_a': 'val_a'}

相同。

除此以外,我还假设熊猫对字典的键进行默认排序(不幸的是,我没有在文档中找到任何提示来证明这一假设),导致

In addition to that I assume that pandas sorts the dictionary's keys descending by default (unfortunately I did not find any hint in the docs in order to prove that assumption) leading to the behavior you encountered.

所以基本动机是对DataFrame中的列进行重新排序。您可以按以下步骤执行此操作

So the basic motivation would be to resort / reorder the columns in your DataFrame. You can do this as follows:

import pandas as pd

data1 = pd.DataFrame({ 'b' : [1, 1, 1], 'a' : [2, 2, 2]})
data2 = pd.DataFrame({ 'b' : [1, 1, 1], 'a' : [2, 2, 2]})
frames = [data1, data2]
data = pd.concat(frames)

print(data)

cols = ['b' , 'a']
data = data[cols]

print(data)

这篇关于pandas.concat中的列顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆