简体   繁体   中英

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.

not sure why:

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

does not work..?

Adding the .values at the end

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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