mati
mati

Reputation: 1145

How to reverse the content of a specific dataframe column in pandas?

I have a pandas dataframe df1 = {'A':['a','b','c','d','e'],'no.':[0,1,2,3,4]}, df1 = pd.DataFrame(df1,columns=['A','no.']) where I would like to reverse in place the content of the second column with the result being like that: df2 = {'A':['a','b','c','d','e'],'no.':[4,3,2,1,0]} df2 = pd.DataFrame(df2,columns=['A','no.'])

Upvotes: 1

Views: 247

Answers (1)

jezrael
jezrael

Reputation: 862781

Convert values to numpy and then indexing for change order:

df1['no.'] = df1['no.'].to_numpy()[::-1]
print (df1)
   A  no.
0  a    4
1  b    3
2  c    2
3  d    1
4  e    0

Upvotes: 1

Related Questions