Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Apply sorting to a list of dataframes Python

I’ve struggling trying to insert a sorting in this code:

all_files = glob.glob(path + "/*.CSV") # To get all csv files disorganized
all_csv = [pd.read_csv(f, sep=',') for f in all_files] # List of dataframes

# I want to sort it by the values of the first column of each dataframe in the all_csv list.

for f in all_csv:
    goal = pd.DataFrame.sort_values(by=(f.iloc[:,0])) #Maybe something like this??

So, Anyone has an idea how can I do this? I’ve looking on other post but does not apply to a undefined column name (a.k.a. f.iloc[:,0] ) or a list of dataframes (I also thought of using dictionaries but I’d like to see if is posible to use with lists).

Thank you 🙂

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

May be useful this ideas: link, link

>Solution :

You can index df.columns for individual dataframes:

goal = df.sort_values(by=df.columns[0])

For the entire list of dataframes, you can use list comprehension:

all_csv = [df.sort_values(by=df.columns[0]) for df in all_csv]

Suppose you had a dataframe that looked like:

   a  b
0  2  1
1  3  2
2  1  3

Then when you run:

df = df.sort_values(by=df.columns[0])

df becomes:

   a  b
2  1  3
0  2  1
1  3  2
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading