Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.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_Pandas_Numpy - Fatal编程技术网

Python通过计算条件进行排序,然后通过单独的列进行排序

Python通过计算条件进行排序,然后通过单独的列进行排序,python,pandas,numpy,Python,Pandas,Numpy,我正在寻找排名顺序的数据帧。我想对4组条件进行排序,然后针对每一个条件,按人的x列对数据帧进行排序 我的数据帧输入: Product indicator_1 indicator_2 indicator_3 #_people A Y Y Y 500 B Y N N 600 C N Y N

我正在寻找排名顺序的数据帧。我想对4组条件进行排序,然后针对每一个条件,按人的x列对数据帧进行排序

我的数据帧输入:

 Product indicator_1 indicator_2 indicator_3 #_people
A        Y            Y          Y           500
B        Y            N          N           600
C        N            Y          N           1000
D        N            Y          N           5000
E        N            Y          Y           200
F        N            N          Y           500
G        N            N          N           600
H        N            N          N           500
预期产出:

 Product indicator_1 indicator_2 indicator_3 #_people
B        Y            N          N           600
A        Y            Y          Y           500
D        N            Y          N           5000
C        N            Y          N           1000
E        N            Y          Y           200
G        N            N          N           600
H        N            N          N           500
F        N            N          Y           700
例如:

如果指示符_1='Y',则首先显示这些行。既然这些行是第一行,那么就按下降人数对它们进行排序。如果指示符_1='N',则这些行根本不显示 如果指示符_2='Y',则第二次显示这些行。按人降序排序。 如果指标_2='N'与上面的指标2和指标_3='N'相同,则将这些行显示在第三行。按下降人数排序。 如果指标_2='N'与上面的指标2相同,指标_3='Y',则将这些行显示在第四行。按下降人数排序。
我想的另一个选择是创建单独的子数据帧,然后合并它们?不确定最有效的选项。

您的数据、预期输出中有错误,您有一个700,它来自哪里

您试图解决的问题不是一步排序问题。您给出的条件在某种程度上是矛盾的:

1st condition indicator_1=="Y", you want to ascending sort Num_people, 
2nd condition indicator_1=="N", you want to descending sort indicator_2
这是我的方法。您需要执行两个步骤: 1.排序指示器_1优先 2.选择指示符1==Y,用于一种排序;然后,指示符1==N表示另一种排序

import pandas as pd
Product_ = ["A", "B", "C", "D", "E", "F", "G", "H"]
indicator_1 = ["Y", "Y", "N", "N", "N", "N", "N", "N"]
indicator_2 = ["Y", "N", "Y", "Y", "Y", "N", "N", "N"]
indicator_3 = ["Y", "N", "N", "N", "Y", "Y", "N", "N"]
Num_people = [500, 600, 1000, 5000, 200, 500, 600, 500]

df = pd.DataFrame({"Product_": Product_, "indicator_1": indicator_1, "indicator_2": indicator_2, "indicator_3": indicator_3, "Num_people": Num_people})
# print(df)
#  Product indicator_1 indicator_2 indicator_3 #_people
# A        Y            Y          Y           500
# B        Y            N          N           600
# C        N            Y          N           1000
# D        N            Y          N           5000
# E        N            Y          Y           200
# F        N            N          Y           500
# G        N            N          N           600
# H        N            N          N           500

temp = df.sort_values(["indicator_1", "Num_people"], ascending = [False, False])

temp_1 = temp[temp.indicator_1=="Y"]
temp = pd.concat([temp_1, temp[temp.indicator_1=="N"].sort_values(["indicator_2", "indicator_3", "Num_people"], ascending = [False, True, False])])
print(temp)
输出为:

  Product_ indicator_1 indicator_2 indicator_3  Num_people
1        B           Y           N           N         600
0        A           Y           Y           Y         500
3        D           N           Y           N        5000
2        C           N           Y           N        1000
4        E           N           Y           Y         200
6        G           N           N           N         600
7        H           N           N           N         500
5        F           N           N           Y         500

希望这能有所帮助。

事实上很难解释,但不是真的。只需提供您的数据帧表,以显示您拥有的内容和希望获得的内容。您好,cexcelc,如果您能提供一个示例数据帧,您希望在其中执行此操作,以及您希望输出的外观,这将非常有帮助。在当前状态下,您的问题有被关闭的风险。听起来您想用它来构建一个指示优先级的新列,然后按该列对数据帧进行排序,然后人们下降HI@abhg我修改了我的帖子HI@d_kennetz我修改了我的帖子