How to search for dataframes and sort each one accordingly?

时间:2019-05-31 11:27:00

标签: python python-3.x pandas

I have dataframes in my local named like s1_down_threshold, s1_up_threshold, s2_down_threshold,s2_down_threshold, s19_down_threshold, s19_down_threshold and so on.

I would like to sort the dataframes having 'down_threshold' in their names in descending order based on one column and the dataframes having 'up_threshold' in their names in ascending order based on the same column.

I know that I can use .sort_values() for each and every one of them, but I would like to know if there is a more efficient way to do it?

I was hoping for something as follows:

Going through the names of all the dataframes in my local and then finding the dataframes with 'down_threshold' in their names and sorting them accordingly and then repeating the same process for 'up_threshold'

Edit 1:

enter image description here

2 个答案:

答案 0 :(得分:1)

您可以先将数据框命名,然后再将其添加到数据框字典中,如下所示:

import numpy as np
import pandas as pd
import json

#using sample data

data = {'id': ['A', 'B', 'C', 'D'], 'value': [2000, 600, 400, 3000]}

df=pd.DataFrame(data)
df1 =df.copy()
df2=df.copy()
df3=df.copy()
df4=df.copy()

DataFrameDict=[]

df1.name='s1_down_threshold'
DataFrameDict.append(df1)
df2.name='s2_down_threshold'
DataFrameDict.append(df2)
df3.name='s1_up_threshold'
DataFrameDict.append(df3)
df4.name='s2_up_threshold'
DataFrameDict.append(df4)

for i in range(len(DataFrameDict)):
    if ('down' in DataFrameDict[i].name):
        print (DataFrameDict[i].name,'sorted down')
        DataFrameDict[i].sort_values(by='value', ascending=False,inplace=True)
    elif ('up' in DataFrameDict[i].name):
        print (DataFrameDict[i].name,'sorted up')
        DataFrameDict[i].sort_values(by='value', ascending=True,inplace=True)

>>> DataFrameDict
[  id  value
3  D   3000
0  A   2000
1  B    600
2  C    400,   
   id  value
3  D   3000
0  A   2000
1  B    600
2  C    400,   
   id  value
2  C    400
1  B    600
0  A   2000
3  D   3000,   
   id  value
2  C    400
1  B    600
0  A   2000
3  D   3000]

答案 1 :(得分:0)

如果所有df文件都以csv格式保存在同一文件夹中,则可以使用os库导入所有文件,并使用split函数确定是否以升序或降序对它们进行排序。

这是它的样子:

import os

for file in os.listdir('./folder1/'):
    pd.read_csv('folder1/' + file)
    if file.split('.')[0].split('_')[1] =='down':
        df.sort_values(ascending = False, inplace = True)
    elif file.split('.')[0].split('_')[1] =='up':
        df.sort_values(ascending = True, inplace = True)
    df.to_csv('folder1/' + file)

如果本地目录中还有其他文件,则可以在if和elif条件下读取csv。