2021年2月3日星期三

Sort a pandas DataFrame based on indices of another sorted DataFrame

There are two pandas DataFrames

  • A sized 10 x 4 (4 columns)
  • B sized 10 x 2 (2 columns)

Both of their indices (row numbers) are identical, running from 1 to 10 in ascending order.

But If I sort B according to its first column using B.sort_values(by='column 1'), its indices will change to something looking disordered, no longer straight1 to 10.

How can I re-sort A the same way that B was sorted, so that its indices are also in the same (dis-)ordering?

https://stackoverflow.com/questions/66037980/sort-a-pandas-dataframe-based-on-indices-of-another-sorted-dataframe February 04, 2021 at 09:04AM

没有评论:

发表评论