Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何搜索数据帧并相应地对每个数据帧进行排序?_Python_Python 3.x_Pandas - Fatal编程技术网

Python 如何搜索数据帧并相应地对每个数据帧进行排序?

Python 如何搜索数据帧并相应地对每个数据帧进行排序?,python,python-3.x,pandas,Python,Python 3.x,Pandas,我的本地数据帧名为s1\u down\u threshold,s1\u up\u threshold,s2\u down\u threshold,s2\u down\u threshold,s19\u down\u threshold等等 我想根据一列按降序排列名称中有“down_threshold”的数据帧,根据同一列按升序排列名称中有“up_threshold”的数据帧 我知道我可以使用.sort_values()对每一个值进行排序,但我想知道是否有更有效的方法 我希望得到如下结果: 检查m

我的本地数据帧名为
s1\u down\u threshold
s1\u up\u threshold
s2\u down\u threshold
s2\u down\u threshold
s19\u down\u threshold
等等

我想根据一列按降序排列名称中有“down_threshold”的数据帧,根据同一列按升序排列名称中有“up_threshold”的数据帧

我知道我可以使用
.sort_values()
对每一个值进行排序,但我想知道是否有更有效的方法

我希望得到如下结果:

检查my local中所有数据帧的名称,然后找到名称中带有“down_threshold”的数据帧,并对其进行相应排序,然后对“up_threshold”重复相同的过程

编辑1:


如果所有df都以csv格式保存在同一文件夹中,则可以使用操作系统库导入所有df,并使用拆分功能决定是升序还是降序排序

下面是它可能的样子:

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

您可以在将数据帧添加到数据帧字典之前对其命名,如下所示:

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]

嘿@anky_91,我将dfs保存在一个dict中。现在我如何在这个字典中找到名称中包含
down_threshold
up_threshold
的数据帧?好的,所以我创建了两个不同的dict,一个用于down_threshold,另一个用于up_threshold,并根据您的建议对其进行排序。现在,有没有一种方法可以让这两个不同的dict中的数据帧替换本地dict中的数据帧?我不确定我是否明白你的问题。你能解释一下你的意思吗?当地的,你的意思是什么?您需要将它们放在任何目录中的文件中。我很困惑,对不起,我不知道怎么做,对不起。通常,每当我使用多个dfs并引用我在任何df上完成工作的键时,我都会将dfs存储在dict中。干杯!!:)@anky_91他所有的df都有相同的格式(name_down/up_threshold),这允许我们识别文件是命名为up还是down,并按照顺序进行排序,但您是对的,它不会搜索类似的字符串