data9 = pd.DataFrame([
[1, 2, 3, '03:10:20:170', 'NEW', 90.1060, 'Agency'],
[1, 2, 3, '03:10:20:144', 'Trade', 90.1050, 'Principal'],
[1, 2, 3, '03:10:20:120', 'NEW', 90.1022, 'Agency'],
[1, 2, 3, '03:10:20:100', 'NEW', 90.1070, 'Agency'],
[1, 2, 3, '03:10:20:155', 'NEW', 90.1051, 'Principal']
], columns=['A', 'B','C','D','E','F','G'])
Python选择排序数据框行
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python选择排序数据框行相关的知识,希望对你有一定的参考价值。
import pandas as pd
data9 = pd.DataFrame([[1, 2, 3, 03:10:20:170, 'NEW',90.1060,'Agency'], [1, 2, 3, 03:10:20:144, 'Trade',90.1050,'Principal'], [1, 2, 3, 03:10:20:120, 'NEW',90.1022,'Agency'],[1, 2, 3, 03:10:20:100, 'NEW',90.1070,'Agency'], [1, 2, 3, 03:10:20:155, 'NEW',90.1051,'Principal']], columns=['A', 'B','C','D','E','F','G'])
我必须在数据框中找到满足这些条件的行:我想选择一个排序行,以便:OrderDirection为“卖”,那么OrderType中的“ PRINCIPAL”元素的价格及其在OrderStatus列中与之对应的“ TRADE”元素应小于价格。 OrderType列中的“ AGENCY”元素以及OrderStatus列中的相应“ NEW”元素。因此,结果表仅包含以下行。为此,它必须遍历整个数据框并找到满足上述条件的所有行集。]
[1, 2, 3, 03:10:20:120, 'NEW',90.1022,'Agency'] [1, 2, 3, 03:10:20:144, 'Trade',90.1050,'Principal']
我收到错误:KeyError:“标签[True]不在[index]中]'>我该如何解决?下面的代码:
def selection_sort(nums):
# This value of i corresponds to how many values were sorted
for i, row in nums.iterrows():
# We assume that the first item of the unsorted segment is the smallest
lowest_value_index = i
# This loop iterates over the unsorted items
for j in (i + 1, range(len(nums.F))):
if row.loc[row['G'] == 'Agency', 'F'].iloc[lowest_value_index] > row.loc[row['G'] == 'Principal', 'F' ].iloc[j]:
lowest_value_index = j
# Swap values of the lowest unsorted element with the first unsorted
# element
row.loc[row['G'] == 'Principal', 'F'].iloc[i], row.loc[row['G'] == 'Agency', 'F'].iloc[lowest_value_index] = row.loc[row['G'] == 'Agency' , 'F'].iloc[lowest_value_index], row.loc[row['G'] == 'Principal', 'F'].iloc[i]
selection_sort(data19)
将熊猫作为pd data9 = pd.DataFrame([[1,2,3,03:10:20:170,'NEW',90.1060,'Agency'],[1,2,3,03:10: 20:144,'Trade',90.1050,'Principal'],[1,2,3,03:10:20:120,'NEW',90.1022,'Agency'],[1,2,...] >
您可以尝试.sort_values()和.rank()。这是我从您提供的数据框中得到的内容(datetime字段需要用引号引起来,BTW)。按F和G列排序,但是您可以为名义值添加临时枚举,以确保按照您的目的将其按正确的方向排序。
In [0]: data9 = data9.sort_values(by=["F","G"], ascending=[True, True])
调用data9以检查结果:
In [1]: data9 Out[1]: A B C D E F G 2 1 2 3 03:10:20:120 NEW 90.1022 Agency 1 1 2 3 03:10:20:144 Trade 90.1050 Principal 4 1 2 3 03:10:20:155 NEW 90.1051 Principal 0 1 2 3 03:10:20:170 NEW 90.1060 Agency 3 1 2 3 03:10:20:100 NEW 90.1070 Agency
然后,我们可以在“ F”列上进行排名(您可以使用更多列,只需将它们包括在列表中即可,如使用sort一样)。之后,我们仅使用条件来选择前2个(任何低于3个),并产生与您的预期输出相似的结果。
In [1]: data9.loc[data9.loc[:, "F"].rank() < 3.0, :] Out[1]: A B C D E F G 2 1 2 3 03:10:20:120 NEW 90.1022 Agency 1 1 2 3 03:10:20:144 Trade 90.1050 Principal
data9 = pd.DataFrame([
[1, 2, 3, '03:10:20:170', 'NEW', 90.1060, 'Agency'],
[1, 2, 3, '03:10:20:144', 'Trade', 90.1050, 'Principal'],
[1, 2, 3, '03:10:20:120', 'NEW', 90.1022, 'Agency'],
[1, 2, 3, '03:10:20:100', 'NEW', 90.1070, 'Agency'],
[1, 2, 3, '03:10:20:155', 'NEW', 90.1051, 'Principal']
], columns=['A', 'B','C','D','E','F','G'])
以上是关于Python选择排序数据框行的主要内容,如果未能解决你的问题,请参考以下文章