简体   繁体   English

向上移动行并重置索引熊猫数据框

[英]Move row up and reset index pandas dataframe

I have a dataframe with the following columns.我有一个包含以下列的数据框。 need to sortby tr_date and move the 6th index row to 1st index.需要按 tr_date 排序并将第 6 个索引行移动到第 1 个索引。

original datafarame

index tr_date      val_date    des         con    cr   dr  bal
0      05-06-2020   05-06-2020  JH876875    NEFT    0   500 500
1      02-07-2020   02-07-2020  45546       MPS    100  0   400
2      02-07-2020   02-07-2020  45546       IMPS    20  0   380
3      22-07-2020   20-07-2020  AASADD      with    200 0   -320
4      28-07-2020   15-07-2020  876876      withdr  0   300 -20
5      03-08-2020   01-08-2020  BCGFD       NEFT    200 0   -220
6      02-07-2020   02-09-2020  23          man     500 0   -120

Expected output:

index tr_date       val_date    des         con    cr   dr  bal
0     05-06-2020    05-06-2020  JH876875    NEFT    0   500 500
1     02-07-2020    02-09-2020  23          man     500 0   -120
2     02-07-2020    02-07-2020  45546       MPS    100  0   400
3     02-07-2020    02-07-2020  45546       IMPS    20  0   380
4     22-07-2020    20-07-2020  AASADD      with    200 0   -320
5     28-07-2020    15-07-2020  876876      withdr  0   300 -20
6     03-08-2020    01-08-2020  BCGFD       NEFT    200 0   -220

this code works for changing the rows:此代码适用于更改行:

df.iloc[6], df.iloc[1] = df.iloc[1], df.iloc[6]

greetings Jan问候简

This should work:这应该有效:

df=df.sort_values(by='tr_date').reset_index()

If you want to secondarily sort on other columns, just add them on by parameter (eg by=['tr_date', 'des'] )如果您想对其他列进行二次排序,只需按参数添加它们(例如by=['tr_date', 'des']

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

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