0

How do I reverse the rows in a column using pandas, as in I want to change the value of the last row, row 4 and switch it with row 0. so instead of row:0 equaling 1 it would equal to 10.

Code associated with the code down below:

import pandas as pd
data =pd.read_csv('input.csv')
#printing out a single column
set_val =data['sets']
set_val

sets dataset(row number, value) output:

0       1
1       2
2       3
3       4
4       10
1
  • 1
    kindly post your expected output Commented Dec 14, 2020 at 6:08

1 Answer 1

1

If need swap first and last value of column sets use:

data.loc[data.index[[0, -1]], 'sets'] = data.loc[data.index[[-1, 0]], 'sets'].to_numpy()

Or:

data.iloc[[0, -1], data.columns.get_loc('sets')] = 
data.iloc[[-1, 0], data.columns.get_loc('sets')].to_numpy()
print (data)
   sets
0    10
1     2
2     3
3     4
4     1

If need reverse all values:

data1 = data.iloc[::-1]
print (data1)
   sets
4    10
3     4
2     3
1     2
0     1
Sign up to request clarification or add additional context in comments.

1 Comment

Is there a way i can reverse all the data's without also reversing the rows, like row 0 =10 , row 1 =4 , row 2 =3 , row 3 =2 and row 4 =1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.