gpt4 book ai didi

python - Pandas:是否有一种通过提供索引标签列表来对行进行排序的 native 方法?

转载 作者:太空宇宙 更新时间:2023-11-04 10:01:24 24 4
gpt4 key购买 nike

让我们使用这个数据框:

import pandas as pd
L0 = ['d','a','b','c','d','a','b','c','d','a','b','c']
L1 = ['z','z','z','z','x','x','x','x','y','y','y','y']
L2 = [1,6,3,8,7,6,7,6,3,5,6,5]
df = pd.DataFrame({"A":L0,"B":L1,"C":L2})
df = df.pivot(columns="A",index="B",values="C")

旋转后,列和行按字母顺序排列。

重新排序列很容易,可以使用列标签的自定义列表来完成:

df = df[['d','a','b','c']]

但是重新排序行没有这样直接的功能,我能想到的最优雅的方法是使用列标签功能并来回转置:

df = df.T[['z','x','y']].T

这样做,例如完全没有效果:

df.loc[['x','y','z'],:] = df.loc[['z','x','y'],:]

是否没有通过提供自定义索引标签列表来直接对数据框的行进行排序的方法?

最佳答案

您可以使用 reindexreindex_axis ,什么比 loc 更快:

对于索引:

idx = ['z','x','y']
df = df.reindex(idx)
print (df)
A a b c d
B
z 6 3 8 1
x 6 7 6 7
y 5 6 5 3

或者:

idx = ['z','x','y']
df = df.reindex_axis(idx)
print (df)
A a b c d
B
z 6 3 8 1
x 6 7 6 7
y 5 6 5 3

作为ssm指出:

df = df.loc[['z', 'x', 'y'], :]
print (df)
A a b c d
B
z 6 3 8 1
x 6 7 6 7
y 5 6 5 3

对于列:

cols = ['d','a','b','c']
df = df.reindex(columns=cols)
print (df)
A d a b c
B
x 7 6 7 6
y 3 5 6 5
z 1 6 3 8

cols = ['d','a','b','c']
df = df.reindex_axis(cols, axis=1)
print (df)
A d a b c
B
x 7 6 7 6
y 3 5 6 5
z 1 6 3 8

两者:

idx = ['z','x','y']
cols = ['d','a','b','c']
df = df.reindex(columns=cols, index=idx)
print (df)
A d a b c
B
z 1 6 3 8
x 7 6 7 6
y 3 5 6 5

时间:

In [43]: %timeit (df.loc[['z', 'x', 'y'], ['d', 'a', 'b', 'c']])
1000 loops, best of 3: 653 µs per loop

In [44]: %timeit (df.reindex(columns=cols, index=idx))
1000 loops, best of 3: 402 µs per loop

只有索引:

In [49]: %timeit (df.reindex(idx))
The slowest run took 5.16 times longer than the fastest. This could mean that an intermediate result is being cached.
1000 loops, best of 3: 271 µs per loop

In [50]: %timeit (df.reindex_axis(idx))
The slowest run took 6.50 times longer than the fastest. This could mean that an intermediate result is being cached.
1000 loops, best of 3: 252 µs per loop


In [51]: %timeit (df.loc[['z', 'x', 'y']])
The slowest run took 5.51 times longer than the fastest. This could mean that an intermediate result is being cached.
1000 loops, best of 3: 418 µs per loop

In [52]: %timeit (df.loc[['z', 'x', 'y'], :])
The slowest run took 4.87 times longer than the fastest. This could mean that an intermediate result is being cached.
1000 loops, best of 3: 542 µs per loop

def pir(df):
idx = ['z','x','y']
a = df.index.values.searchsorted(idx)
df = pd.DataFrame(
df.values[a],
df.index[a], df.columns
)
return df

In [63]: %timeit (pir(df))
The slowest run took 7.75 times longer than the fastest. This could mean that an intermediate result is being cached.
10000 loops, best of 3: 91.8 µs per loop

关于python - Pandas:是否有一种通过提供索引标签列表来对行进行排序的 native 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43384397/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com