反转 Pandas 数据框的列顺序,而不更改列标签。

huangapple go评论80阅读模式
英文:

Reverse the order of columns without changing the column labels pandas dataframe

问题

需要反转我的pandas数据框的顺序。但是使用以下代码:

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})
df = df.iloc[:, ::-1]

这也会反转列标签的顺序。如何只反转数据并保留列标签?我希望得到:

   A  B  C
0  7  4  1
1  8  5  2
2  9  6  3
英文:

I need to reverse the order of my pandas dataframe. But using the following code:

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})
df = df.iloc[:, ::-1]

also reverses the order of the column labels. How can i reverse only the data and maintain the column labels? I expect to get:

   A  B  C
0  7  4  1
1  8  5  2
2  9  6  3

答案1

得分: 4

If large DataFrame and performance is important assign to new Dataframe by constructor:

如果有大型的DataFrame并且性能很重要可以使用构造函数分配给新的DataFrame

df = pd.DataFrame(np.random.randint(10, size=(1000,1000))).add_prefix('col')

In [108]: %timeit df[:] = df.values[:, ::-1]
5.19毫秒 ± 555微秒 每次循环平均值 ± 7次的标准偏差每次循环100次

In [109]: %timeit df[:] = df.iloc[:, ::-1].to_numpy()
5.34毫秒 ± 288微秒 每次循环平均值 ± 7次的标准偏差每次循环100次

In [110]: %timeit df.iloc[:, ::-1].set_axis(df.columns, axis=1)
2.42毫秒 ± 357微秒 每次循环平均值 ± 7次的标准偏差每次循环100次

In [111]: %timeit pd.DataFrame(df.iloc[:, ::-1].to_numpy(), index=df.index, columns=df.columns)
102微秒 ± 3.45微秒 每次循环平均值 ± 7次的标准偏差每次循环10000次

In [112]: %timeit pd.DataFrame(df.to_numpy()[:, ::-1], index=df.index, columns=df.columns)
43.7微秒 ± 240纳秒 每次循环平均值 ± 7次的标准偏差每次循环10000次
英文:

If large DataFrame and performance is important assign to new Dataframe by constructor:

df = pd.DataFrame(np.random.randint(10, size=(1000,1000))).add_prefix('col')
# print (df)

In [108]: %timeit df[:] = df.values[:, ::-1]
5.19 ms ± 555 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [109]: %timeit df[:] = df.iloc[:, ::-1].to_numpy()
5.34 ms ± 288 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [110]: %timeit df.iloc[:, ::-1].set_axis(df.columns, axis=1)
2.42 ms ± 357 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [111]: %timeit pd.DataFrame(df.iloc[:, ::-1].to_numpy(), index=df.index,columns=df.columns)
102 µs ± 3.45 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [112]: %timeit pd.DataFrame(df.to_numpy()[:, ::-1], index=df.index, columns=df.columns)
43.7 µs ± 240 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

答案2

得分: 2

要在原地修改,请使用:

df[:] = df.iloc[:, ::-1].to_numpy()

或者,要创建一个新对象,请使用:

out = df.iloc[:, ::-1].set_axis(df.columns, axis=1)
英文:

To modify in place, use:

df[:] = df.iloc[:, ::-1].to_numpy()

Or, for a new object:

out = df.iloc[:, ::-1].set_axis(df.columns, axis=1)

答案3

得分: 2

使用最短的分配语句:

df[:] = df.values[:, ::-1]

   A  B  C
0  1  4  7
1  2  5  8
2  3  6  9
英文:

With shortest assignment:

df[:] = df.values[:, ::-1]

   A  B  C
0  7  4  1
1  8  5  2
2  9  6  3

huangapple
  • 本文由 发表于 2023年3月7日 19:36:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75661472.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定