English 中文(简体)
如果在安达有分类价值和目标价值(单位=1),如何去掉行?
原标题:How to remove row if grouped value and target value(unique count=1) in pandas?

原材料

RefNo rest1 Pair rest2
A 11 A 11
B 22 E 55
C 33 C 22
C 33 F 66
D 44 D 44
D 44 G 77
D 44 H 88

我想得到......

- 团体:无

- 如果RefNo=Pair & Unique amount(Pair) = 1 ......去除。

结果

RefNo rest1 Pair rest2
B 22 E 55
C 33 C 22
C 33 F 66
D 44 D 44
D 44 G 77
D 44 H 88

给予任何帮助。

问题回答

你们可以通过安达来实现预期的结果。 在这里,删除每一组“不”中“公平”的独特数字等于1和“不”等于“公平”的用语:

import pandas as pd

# Your raw data
data = {
     RefNo : [ A ,  B ,  C ,  C ,  D ,  D ,  D ],
     rest1 : [11, 22, 33, 33, 44, 44, 44],
     Pair : [ A ,  E ,  C ,  F ,  D ,  G ,  H ],
     rest2 : [11, 55, 22, 66, 44, 77, 88]
}

df = pd.DataFrame(data)

# Group by  RefNo  and filter the groups
filtered_groups = df.groupby( RefNo ).filter(lambda x: x[ Pair ].nunique() > 1 or x[ RefNo ].nunique() > 1)

# Output the result
print(filtered_groups)

该法典将数据框架按“RefNo”加以分类,然后使用“lambda”功能对各群体进行过滤。 如果每个组别中独一无二的“公平”数大于1,或每个组别中独一无二的“不”数值大于1,则“公平”功能检查。 如果其中任一条件都属实,则该群体将被保存在过滤的数据框架内。 否则,该群体(及其相应增长)就从结果中删除。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签