2017-04-13 66 views
2

好吧,所以我有一个预先创建的数据框,我尝试追加一个列表。在简化版本中,问题在下面。熊猫 - 在现有数据框中追加列表,与列相同的顺序

df = pd.DataFrame({'One': [], 
       'Another' : [], 
       'Third' : [], 
       'Last' : []}) 

然后我做一些东西与列表:

new_obj = [] 
new_obj.append('1') 
new_obj.append('2') 
#I have to do the extend operation here 
new_obj.extend(['3','4']) 

现在我只想补充我的new_obj列表进入我的数据帧,对象以相同的顺序,因为我希望他们在数据帧。

所以我只是做:

df.loc[len(df.index)] = new_obj 

而作为结果,我有:

Another Last One Third 
0  1 2 3  4 

为什么?为什么它会将列顺序改为失去字词的顺序。如何在追加时保留它?

回答

2

注意dict在Python是由无序性质。为了指定DataFrame您可以编写(或用户collections.OrderedDict)正确的顺序:

df = pd.DataFrame({'One': [], 
        'Another' : [], 
        'Third' : [], 
        'Last' : []}, columns=["One", "Another", "Third", "Last"]) 

在另一方面,如果你真的不关心在DataFrame的顺序,你可以只明确定义列的诠释,他list只需使用dict不是您要添加:

new_obj = {"One" : 1, "Anohter" : 2, "Third" : 3, "Last" : 4} 
df.loc[len(df.index)] = new_obj 
2

你行df.loc[len(df.index)] = new_obj没有改变列的顺序。

字典键是无序的,所以当你通过字典来pd.DataFrame()来创建数据框,您的列不一定是你写的顺序。

尝试使用此方法确认:

df = pd.DataFrame({'One': [], 
       'Another' : [], 
       'Third' : [], 
       'Last' : []}) 

df.columns 

指数([ '的另一种', '上次', '一个', '第三'],D型细胞= '对象')

...如果你关心列的顺序,而不是初始化DF这样的:

columns = ['one', 'another', 'third', 'last'] 
df = pd.DataFrame(columns=columns) 
1

正如其他的答案中提到,dictonary键是没有顺序的。如果你想订购的dict使用orderddict作为

One Another Third Last 
0 1  2  3 4 
1

图所示

import pandas as pd 
import collections 

mydict = collections.OrderedDict((('One', []), 
       ('Another', []), 
       ('Third', []), 
       ('Last', []))) 

df = pd.DataFrame.from_dict(mydict) 

new_obj = [] 
new_obj.append('1') 
new_obj.append('2') 
#I have to do the extend operation here 
new_obj.extend(['3','4']) 
df.loc[len(df.index)] = new_obj 
print df 

结果使用append与参数ignore_index=True

df = pd.DataFrame(columns='One Another Third Last'.split()) 

new_obj = [] 
new_obj.append('1') 
new_obj.append('2') 
#I have to do the extend operation here 
new_obj.extend(['3','4']) 

# notice I created a `pd.Series` with `df.columns` as the index 
df.append(pd.Series(new_obj, df.columns), ignore_index=True) 

    One Another Third Last 
0 1  2  3 4