How to Sort All Dataframes in a List of Dataframes on the Same Column

How to sort all dataframes in a list of dataframes on the same column?

Use lapply:

sorted_dataframes_list <- lapply(dataframes_list, function(df){
df[order(df$enrichment),]
})

sort the order of dataframes in a list of dataframes based on a value in each dataframe

If you want to sort the rows of each dataframe, you need to provide the exact format of your datetime, and you should sort in place:

for d in df_list:
d['time'] = pd.to_datetime(d['time'], format='%Y-%m-%d %H.%M.%S.%f')
d.sort_values(by='time', inplace=True)

Or, if you want to sort the dataframes in the list, which is completely different, use:

df_list.sort(key=lambda d: d['time'].iloc[0])

You should be able to sort using the string due to your particular format (assuming YYYY-MM-DD).

To ensure sorting on datetime (for example if the format was MM-DD-YYYY):

df_list.sort(key=lambda d: pd.to_datetime(d['time'].iloc[0], format='%Y-%m-%d %H.%M.%S.%f'))

sorting rows of data frames which are in a list in R

To expand my comment above into answer, you can use lapply to apply a sort to each data frame in your list:

lapply(ds, function(x) x[order(x$E, x$F), ])

Sort two columns in ascending order for each dataframe in a list using a for loop in r

We can either use lapply in base R

Split_Data <- lapply(Split_Data, function(x) x[order(-x$Scale_1, -x$Scale_2),])

or with map from purrr and arrange (by default it is in ascending order)

library(purrr)
library(dplyr)
Split_Data <- map(Split_Data, ~ .x %>%
arrange(desc(Scale_1), desc(Scale_2)))

Sort list of pandas-dataframes by row count?

Use the built-in function sorted.

lst = sorted([df1, df2, df3], key=len)

How to sort dataframes within a list by column value within each dataframe?

A base R option would be to extract the min value from list and order it

out <- list[order( sapply(list, function(x) min(x$C)))]
out
#[[1]]
# A B C
#1 1 2 0.2
#2 2 3 0.2
#3 3 4 0.2
#4 4 5 0.2

#[[2]]
# A B C
#1 1 1 0.5
#2 2 2 0.5
#3 3 3 0.5

#[[3]]
# A B C
#1 1 2 0.9

NOTE: list is a function, so it is better not to name the identifier object with the function name

Sort index list in same way as list of pandas dataframes is sorted by length in python?

Your initial try is good, just need the right key function to the sort. Here's how it can be done.

lst = [df1, df2, df3]  # Given the list of dataframes...

# Decorate each dataframe with its initial index
# and sort.
# Use a key that takes the length of the dataframe still.

# Input here: [(1, df1), (2, df2), (3, df3)]
# Output here: [(3, df3), (1, df1), (2, df2)] (or whatever is the correct order)
lst_sort = sorted(enumerate(lst, start=1), key=lambda tup: len(tup[1]))

# now split the index and dataframe lists apart again if needed
# by using a trick where it feels like we use zip in reverse
indexes, dataframes = zip(*lst_sort)

If you want more examples, see the Sorting HOWTO in the Python docs.

Note: I've used start=1 here to get 1 as the first index as in the question, but indexes in Python generally start at 0 by convention and because lists are indexed that way, so do consider using 0-based indexing if that's more convenient.

Sorting dataframe by multiple columns through list of column names


The following solution works for me! Other proposed solutions I tried failed to sort by two variables in a given vector simultaneously.

DT <-data.frame(avar = c(1,4,3,1), bvar = c("a","f","s","b"), cvar = c(3,4,5,2))

sort1 <-c("avar", "cvar")
sort2 <-c("avar", "bvar")
sorting <-list(sort1,sort2)
DT2<-list()

for (i in 1:2) {
#THE FOLLOWING SOLUTION WORKS!!!
DT2[[i]] <- DT[do.call(order,DT[as.character(sorting[[i]])]),]
}

sort list of dataframes in python by highest value in shared column


from pandas import DataFrame
import pandas as pd
dict1 = {"id":[1,2,3],"age":[10,20,60]}
dict2 = {"id":[4,5,6],"age":[10,20,40]}

df1 = DataFrame.from_dict(dict1)
df2 = DataFrame.from_dict(dict2)

dflist = [df1,df2]


sorteddflist= sorted(dflist,key=lambda x:x["age"].max(axis=0))
for i in sorteddflist:
print(i)


Related Topics



Leave a reply



Submit