有没有办法根据我的个人偏好重新排序pandas数据框中的列(即不按字母顺序或数字排序,但更像是遵循某些约定)?
简单的例子:
frame = pd.DataFrame({
'one thing':[1,2,3,4],
'second thing':[0.1,0.2,1,2],
'other thing':['a','e','i','o']})
Run Code Online (Sandbox Code Playgroud)
产生这个:
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
Run Code Online (Sandbox Code Playgroud)
但相反,我想这样:
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
Run Code Online (Sandbox Code Playgroud)
(请提供一个通用的解决方案,而不是针对这种情况.非常感谢.)
A.K*_*Kot 124
只需输入列名称即可自行选择订单.注意双括号:
frame = frame[['column I want first', 'column I want second'...etc.]]
Run Code Online (Sandbox Code Playgroud)
Nod*_*ili 62
你可以用这个:
columnsTitles = ['onething', 'secondthing', 'otherthing']
frame = frame.reindex(columns=columnsTitles)
Run Code Online (Sandbox Code Playgroud)
omr*_*don 22
你也可以这样做 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
Run Code Online (Sandbox Code Playgroud)
此外,您可以获取列的列表:
cols = list(df.columns.values)
Run Code Online (Sandbox Code Playgroud)
输出将产生如下所示:
['x', 'y', 'a', 'b']
Run Code Online (Sandbox Code Playgroud)
然后很容易手动重新排列.
Lal*_* La 17
这是我经常使用的解决方案。当您拥有包含大量列的大型数据集时,您绝对不希望手动重新排列所有列。
您可以并且最有可能想做的是只是对经常使用的前几列进行排序,然后让所有其他列成为自己。这是R中的常用方法。df %>%select(one, two, three, everything())
因此,您可以首先手动键入要排序的列和要位于列表中所有其他列之前的列cols_to_order。
然后,通过组合其余各列来构造新列的列表:
new_columns = cols_to_order + (frame.columns.drop(cols_to_order).tolist())
Run Code Online (Sandbox Code Playgroud)
之后,您可以使用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[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
Run Code Online (Sandbox Code Playgroud)
piR*_*red 11
用列表而不是字典构造它
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
Run Code Online (Sandbox Code Playgroud)
您也可以使用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
Run Code Online (Sandbox Code Playgroud)
尽管这是一个老问题,您也可以使用locand iloc:
frame = frame.loc[:, ['column I want first', 'column I want second', "other thing"]]
frame = frame.iloc[:, [1, 3, 2]]
Run Code Online (Sandbox Code Playgroud)
添加“ columns”参数:
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']
)
Run Code Online (Sandbox Code Playgroud)
尝试索引(因此您不仅需要通用解决方案,因此索引顺序可以是您想要的):
l=[0,2,1] # index order
frame=frame[[frame.columns[i] for i in l]]
Run Code Online (Sandbox Code Playgroud)
现在:
print(frame)
Run Code Online (Sandbox Code Playgroud)
是:
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
140333 次 |
| 最近记录: |