Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.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 - Fatal编程技术网

Python 在数据帧中使用条件对值进行排序

Python 在数据帧中使用条件对值进行排序,python,pandas,Python,Pandas,我想根据col1对col2进行降序排序。我觉得答案很简单,但我找不到正确的方法。我将非常感谢你的帮助 数据帧看起来像: Col1 Col2 Col3 AB 5 Blue AB 1 Red AB 2 Green AC 1 Red AC 4 Blue AD 9 Red AD 5 Blue AD 7 Green 所需输出: Col1 Col2 Col3 AB 5 Blue AB 2 Green

我想根据col1对col2进行降序排序。我觉得答案很简单,但我找不到正确的方法。我将非常感谢你的帮助

数据帧看起来像:

Col1 Col2 Col3 
AB   5    Blue
AB   1    Red
AB   2    Green
AC   1    Red
AC   4    Blue
AD   9    Red
AD   5    Blue
AD   7    Green
所需输出:

Col1 Col2 Col3 
 AB   5    Blue
 AB   2    Green
 AB   1    Red
 AC   4    Blue
 AC   1    Red
 AD   9    Red
 AD   7    Green
 AD   5    Blue
我尝试的是:

df = pd.read_csv('data.csv')
df.sort_values(['Col1','Col2'], ascending = False)
上述方法均未给出所需的输出。

执行以下操作:

print(df.sort_values(['Col1', 'Col2'], ascending=[True, False]))
输出

  Col1  Col2   Col3
0   AB     5   Blue
2   AB     2  Green
1   AB     1    Red
4   AC     4   Blue
3   AC     1    Red
5   AD     9    Red
7   AD     7  Green
6   AD     5   Blue
从以下文件:

升序:bool或bool列表,默认为True

Sort ascending vs. descending. Specify list for multiple sort orders. If this is a list of bools, must match the length of the by.
Sort ascending vs. descending. Specify list for multiple sort orders. If this is a list of bools, must match the length of the by.