简体   繁体   English

如何反转熊猫数据框中的行顺序

[英]How to reverse the order of a row in a pandas dataframe

Is it possible to reverse order of a single row in a dataframe: 是否可以反转数据帧中单行的顺序:

from: 从:

   col1   col2   col3
0  65      34     1
1   1       2     3
2   1       2     3

to: 至:

   col1   col2   col3
0   1      34     65
1   1       2     3
2   1       2     3

I have tried creating a new row with reversed values from row 0. 我尝试用行0的相反值创建新行。

not sure why: 不知道为什么:

df.loc[3] = df.iloc[0][::-1]

does not work..? 不起作用..?

Adding the .values at the end 在末尾添加.values

df.iloc[0,:]=df.iloc[0,:][::-1].values
df
Out[783]: 
   col1  col2  col3
0     1    34    65
1     1     2     3
2     1     2     3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM