从字典列表创建DataFrame时指定列的顺序

时间:2019-06-04 16:23:07

标签: python pandas dataframe dictionary

我有一个方法,该方法创建一个列表,结果,然后在其上追加一个dict行,该行具有键(最终数据帧中的列名)和值(最终数据帧中的行值)。然后,我追加该行,并在最后将结果集合转换为df。例如:

results = []
row = {}
row['Name']="alice"
row['Age']=3
results.append(row)
..reset row, fill in values like above, and append more rows..

df = pd.DataFrame(results)

我遇到的问题是列名称已按字母顺序进行排序,因此df如下所示:

df.head()
|Age |  Name  |
|3   | alice  |

有没有一种方法可以将列名指定为我想要的顺序(“名称”,“颜色”)?实际上,我还有更多列,所以我不想这样做:

cols = df.columns
cols = cols[:1] + cols[0:1] 

并手动重新排列。但是,如果我这样做,是仅在列行还是在下面的行周围移动?那么,在移动该列时,“ alice”和下面一行中的3是否也会像预期的那样移动?

3 个答案:

答案 0 :(得分:1)

ext { springBootVersion = '2.1.3.RELEASE' } repositories { maven { url "https://plugins.gradle.org/m2/" } mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") classpath "gradle.plugin.com.boxfuse.client:gradle-plugin-publishing:5.2.4" } } apply plugin: 'java' apply plugin: 'org.springframework.boot' apply plugin: 'io.spring.dependency-management' dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-security' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'com.google.code.gson:gson:2.8.5' runtimeOnly 'org.springframework.boot:spring-boot-devtools' } 属性指定所需排序的列表,并且columns将在创建DataFrame时对列进行重新排序。

DataFrame

或者,pd.DataFrame(results, columns=['Name', 'Age']) Name Age 0 alice 3 也这样做。

DataFrame.from_records

如果您要处理多个列,则可以在不关心其余列的顺序的情况下始终选择执行pd.DataFrame.from_records(results, columns=['Name', 'Age']) Name Age 0 alice 3

我已经写过有关从这篇文章中的记录构造DataFrame的信息:Convert list of dictionaries to a pandas DataFrame


另一个想法,如果顺序不正确,请修复列。

columns=['Name', 'Age', *(row.keys() - {'Name', 'Age'})]

如果最初在“年龄”之后插入,则会在“年龄”之前插入“姓名”。

答案 1 :(得分:1)

IIUC:

df = pd.DataFrame(results,columns=list(row.keys()))
#from collections import OrderedDict :alternative
#columns=[i for i in OrderedDict.fromkeys(row.keys())]
print(df)

    Name  Age
0  alice    3

答案 2 :(得分:0)

According to the docs,从pandas 0.23和python 3.6开始,从字典构建时,将保持顺序。您可以简单地使用OrderedDict

import pandas as pd
from collections import OrderedDict


results = []
row = OrderedDict()

row['Name']="alice"
row['Age']=3
results.append(row)
#..reset row, fill in values like above, and append more rows..

df = pd.DataFrame(results)
print(df.head())

    Name  Age
0  alice    3