Panda's equivalent of R's order() for arranging dataframe columns

Nancy

I'm learning pandas after using R for several years. I would like to arrange the order of columns in a data frame based on the max value in the column. What is the equivalent in pandas of the R example code below?

# R code
test = data.frame(matrix(1:9,ncol = 3))
test[,order(apply(test, 2, max),decreasing = TRUE)]

Here's what I've attempted unsuccessfully in pandas:

# Python code
test = pd.DataFrame({"c":[1,2,3],
                     "a":[4,5,6],
                     "t":[7,8,9]})
test = test.sort_index(axis=1, ascending=False)

Obviously, that just arranges them columns based on the name. I used c, a, t to check for that behavior. How can I replicate what I did in R?

mathematical.coffee

I'm sure there is a more succinct version, but here's one option:

test[test.max(columns=1).order(ascending=False).index]
  • test.max(columns=1) finds the max in each column (like R's apply(test, 2, max))
  • .order(ascending=False).index is like R's order(,,, decreasing=T)
  • test[ column_numbers ] reorders the columns.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python: logical comparing with columns in panda's dataframe

From Dev

Does Spark Dataframe have an equivalent option of Panda's merge indicator?

From Dev

Panda's DataFrame - renaming multiple identically named columns

From Dev

R equivalent of Excel's "Sumif(s)" function across like columns

From Dev

CSS Arranging order of elements when screen width changes it's properties

From Dev

Equivalent of R's createDataPartition in Python

From Dev

What's the equivalent of `cons` in R?

From Dev

Pandas Equivalent of R's which()

From Dev

Equivalent of R's removeSparseTerms in Python

From Dev

equivalent of R's View for Python's pandas

From Dev

What is python's equivalent of R's NA?

From Dev

Python's equivalent for R's dput() function

From Dev

mean of all the columns of a panda dataframe?

From Dev

Converting list in panda dataframe into columns

From Dev

Creating a panda's dataframe out of a single variable that contains a dict

From Dev

Panda's DataFrame double transpose changes numeric types to object

From Dev

How do you filter out rows with NaN in a panda's dataframe

From Dev

how to replace values of selected row of a column in panda's dataframe?

From Dev

Removing duplicates in dataset using panda's dataframe.drop_duplicate()

From Dev

How do you filter out rows with NaN in a panda's dataframe

From Dev

How to order 2 columns by today's date

From Dev

Is there a Python equivalent to R's sample() function?

From Dev

Python equivalent for R's 'zoo' package

From Dev

What is the equivalent of SQL's IN keyword in R?

From Dev

Is there an equivalent to R's negative indexing in Matlab?

From Java

Equivalent of R's factor function in Pandas

From Dev

R's read.table equivalent in Python

From Dev

Python equivalent of R's head and tail function

From Dev

What's the R equivalent of progn in lisp?

Related Related

  1. 1

    Python: logical comparing with columns in panda's dataframe

  2. 2

    Does Spark Dataframe have an equivalent option of Panda's merge indicator?

  3. 3

    Panda's DataFrame - renaming multiple identically named columns

  4. 4

    R equivalent of Excel's "Sumif(s)" function across like columns

  5. 5

    CSS Arranging order of elements when screen width changes it's properties

  6. 6

    Equivalent of R's createDataPartition in Python

  7. 7

    What's the equivalent of `cons` in R?

  8. 8

    Pandas Equivalent of R's which()

  9. 9

    Equivalent of R's removeSparseTerms in Python

  10. 10

    equivalent of R's View for Python's pandas

  11. 11

    What is python's equivalent of R's NA?

  12. 12

    Python's equivalent for R's dput() function

  13. 13

    mean of all the columns of a panda dataframe?

  14. 14

    Converting list in panda dataframe into columns

  15. 15

    Creating a panda's dataframe out of a single variable that contains a dict

  16. 16

    Panda's DataFrame double transpose changes numeric types to object

  17. 17

    How do you filter out rows with NaN in a panda's dataframe

  18. 18

    how to replace values of selected row of a column in panda's dataframe?

  19. 19

    Removing duplicates in dataset using panda's dataframe.drop_duplicate()

  20. 20

    How do you filter out rows with NaN in a panda's dataframe

  21. 21

    How to order 2 columns by today's date

  22. 22

    Is there a Python equivalent to R's sample() function?

  23. 23

    Python equivalent for R's 'zoo' package

  24. 24

    What is the equivalent of SQL's IN keyword in R?

  25. 25

    Is there an equivalent to R's negative indexing in Matlab?

  26. 26

    Equivalent of R's factor function in Pandas

  27. 27

    R's read.table equivalent in Python

  28. 28

    Python equivalent of R's head and tail function

  29. 29

    What's the R equivalent of progn in lisp?

HotTag

Archive