How to order a data frame by one descending and one ascending column?
I used this code to produce your desired output. Is this what you were after?
rum <- read.table(textConnection("P1 P2 P3 T1 T2 T3 I1 I2
2 3 5 52 43 61 6 b
6 4 3 72 NA 59 1 a
1 5 6 55 48 60 6 f
2 4 4 65 64 58 2 b"), header = TRUE)
rum$I2 <- as.character(rum$I2)
rum[order(rum$I1, rev(rum$I2), decreasing = TRUE), ]
P1 P2 P3 T1 T2 T3 I1 I2
1 2 3 5 52 43 61 6 b
3 1 5 6 55 48 60 6 f
4 2 4 4 65 64 58 2 b
2 6 4 3 72 NA 59 1 a
How to sort data by column in descending order in R
You need to use dataframe name as prefix
chickens[order(chickens$feathers),]
To change the order, the function has decreasing
argument
chickens[order(chickens$feathers, decreasing = TRUE),]
How to sort ascending and descending depending on a value in another column in pandas?
If you can assume that your "price"
column will always contain non-negative values, we could "cheat". Assign a negative value to the prices of buy or sell operations, sort, and then calculate the absolute value to go back to the original prices:
If type is
"buy"
, the price remains positive (2 * 1 - 1 = 1). If type is"sell"
, the price will become negative (2 * 0 - 1 = -1).df["price"] = df["price"] * (2 * (df["type"] == "buy").astype(int) - 1)
Now sort values normally. I've included both
"initiator_id"
and"type"
columns to match your expected output:df = df.sort_values(["initiator_id", "type", "price"])
Finally, calculate the absolute value of the
"price"
column to retrieve your original values:df["price"] = df["price"].abs()
Expected output of this operation on your sample input:
initiator_id price type bidnum
0 1 170.81 sell 0
2 2 169.19 buy 0
1 2 170.81 sell 0
4 3 169.19 buy 0
3 3 170.81 sell 0
5 3 70.81 sell 1
9 4 69.19 buy 1
7 4 169.19 buy 0
6 4 170.81 sell 0
8 4 70.81 sell 1
How to sort 2 columns in a Dataframe, one sorted in descending order and the other in alphabetical order corresponding to the 1st column
df = df.sort_values(['Rank', 'Name'],ascending = [False, True])
How to sort a Pandas DataFrame with multiple columns, some in ascending order and others descending order?
The DataFrame.sort_values
method can handle this very easily. Just use the ascending
argument and provide a list of boolean values.
import pandas as pd
my_df = pd.DataFrame({'col1':['a','a','a','a','b','b','b','b','c','c','c','c'],
'col2':[1,1,2,2,1,1,2,2,1,1,2,2],
'col3':[1,2,1,2,1,2,1,2,1,2,1,2]})
my_df = my_df.sort_values(by=['col1','col2','col3'],
ascending=[False, False, True])
Note that the list provided in the ascending
argument must have the same length as the one provided in the by
argument.
R programming, how do I sort data frame by string column in descending order?
We can use decreasing = TRUE
temp[order(as.character(temp$y), decreasing = TRUE),]
R Sort one column ascending, all others descending (based on column order)
We can use arrange
from dplyr
library(dplyr)
arrange(df, Size, desc(A), desc(B), desc(C))
For more number of columns, arrange_
can be used
cols <- paste0("desc(", names(df)[1:3], ")")
arrange_(df, .dots = c("Size", cols))
Related Topics
Texture in Barplot for 7 Bars in R
How to Create a R Timeseries for Hourly Data
Problems Using Foreach Parallelization
How to Use Plyr to Number Rows
How to Not Run an Example Using Roxygen2
How to Change the Na Color from Gray to White in a Ggplot Choropleth Map
Initialize an Empty Tibble with Column Names and 0 Rows
Split a String Vector at Whitespace
Dynamic Height and Width for Knitr Plots
Fixing Set.Seed for an Entire Session
Calculating Weighted Mean and Standard Deviation
How to Get Parameters from Config File in R Script
R: Text Progress Bar in for Loop
Why Do Logicals (Booleans) in R Require 4 Bytes
How to Display Widgets Inline in Shiny
Arrange a Grouped_Df by Group Variable Not Working
How to Extract a Few Random Rows from a Data.Table on the Fly