簡體   English   中英

設置熊貓數據框中的列順序

[英]Set order of columns in pandas dataframe

有沒有辦法根據我的個人喜好對 pandas 數據框中的列進行重新排序(即不是按字母或數字排序,而是更像遵循某些約定)?

簡單的例子:

frame = pd.DataFrame({
        'one thing':[1,2,3,4],
        'second thing':[0.1,0.2,1,2],
        'other thing':['a','e','i','o']})

產生這個:

   one thing other thing  second thing
0          1           a           0.1
1          2           e           0.2
2          3           i           1.0
3          4           o           2.0

但相反,我想要這樣:

   one thing second thing  other thing
0          1           0.1           a
1          2           0.2           e
2          3           1.0           i
3          4           2.0           o

(請提供一個通用的解決方案,而不是針對這種情況。非常感謝。)

只需通過輸入列名自己選擇訂單。 注意雙括號:

frame = frame[['column I want first', 'column I want second'...etc.]]

你可以使用這個:

columnsTitles = ['onething', 'secondthing', 'otherthing']

frame = frame.reindex(columns=columnsTitles)

這是我經常使用的解決方案。 當您擁有包含大量列的大型數據集時,您絕對不想手動重新排列所有列。

您可以並且很可能想要做的只是訂購您經常使用的前幾列,而讓所有其他列成為它們自己。 這是 R 中的常用方法。 df %>%select(one, two, three, everything())

因此,您可以首先手動鍵入要排序的列,並在列表cols_to_order中的所有其他列之前定位。

然后通過組合其余列來構造新列的列表:

new_columns = cols_to_order + (frame.columns.drop(cols_to_order).tolist())

在此之后,您可以按照建議的其他解決方案使用new_columns

import pandas as pd
frame = pd.DataFrame({
    'one thing': [1, 2, 3, 4],
    'other thing': ['a', 'e', 'i', 'o'],
    'more things': ['a', 'e', 'i', 'o'],
    'second thing': [0.1, 0.2, 1, 2],
})

cols_to_order = ['one thing', 'second thing']
new_columns = cols_to_order + (frame.columns.drop(cols_to_order).tolist())
frame = frame[new_columns]

   one thing  second thing other thing more things
0          1           0.1           a           a
1          2           0.2           e           e
2          3           1.0           i           i
3          4           2.0           o           o

你也可以做類似df = df[['x', 'y', 'a', 'b']]

import pandas as pd
frame = pd.DataFrame({'one thing':[1,2,3,4],'second thing':[0.1,0.2,1,2],'other thing':['a','e','i','o']})
frame = frame[['second thing', 'other thing', 'one thing']]
print frame
   second thing other thing  one thing
0           0.1           a          1
1           0.2           e          2
2           1.0           i          3
3           2.0           o          4

此外,您可以通過以下方式獲取列列表:

cols = list(df.columns.values)

輸出將產生如下內容:

['x', 'y', 'a', 'b']

然后很容易手動重新排列。

用列表而不是字典來構造它

frame = pd.DataFrame([
        [1, .1, 'a'],
        [2, .2, 'e'],
        [3,  1, 'i'],
        [4,  4, 'o']
    ], columns=['one thing', 'second thing', 'other thing'])

frame

   one thing  second thing other thing
0          1           0.1           a
1          2           0.2           e
2          3           1.0           i
3          4           4.0           o

您還可以使用 OrderedDict:

In [183]: from collections import OrderedDict

In [184]: data = OrderedDict()

In [185]: data['one thing'] = [1,2,3,4]

In [186]: data['second thing'] = [0.1,0.2,1,2]

In [187]: data['other thing'] = ['a','e','i','o']

In [188]: frame = pd.DataFrame(data)

In [189]: frame
Out[189]:
   one thing  second thing other thing
0          1           0.1           a
1          2           0.2           e
2          3           1.0           i
3          4           2.0           o

添加“列”參數:

frame = pd.DataFrame({
        'one thing':[1,2,3,4],
        'second thing':[0.1,0.2,1,2],
        'other thing':['a','e','i','o']},
        columns=['one thing', 'second thing', 'other thing']
)

嘗試索引(所以你不僅需要一個通用的解決方案,所以索引順序可以是你想要的):

l=[0,2,1] # index order
frame=frame[[frame.columns[i] for i in l]]

現在:

print(frame)

是:

   one thing second thing  other thing
0          1           0.1           a
1          2           0.2           e
2          3           1.0           i
3          4           2.0           o

即使這是一個老問題,您也可以使用lociloc

frame = frame.loc[:, ['column I want first', 'column I want second', "other thing"]]

frame = frame.iloc[:, [1, 3, 2]]

我發現這是最直接和最有效的:

df = pd.DataFrame({
        'one thing':[1,2,3,4],
        'second thing':[0.1,0.2,1,2],
        'other thing':['a','e','i','o']})

df = df[['one thing','second thing', 'other thing']]
df = df.reindex(columns=["A", "B", "C"])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM