我在删除数据框的一些记录方面遇到了麻烦。如果我将某个列分组并检查另一列中的每个列组,而对于每个组都有任何特定值,则如果该特定值不存在,则从第一列中删除了整个组(我们所在的列早期应用组)。 My data look like this:
search_value_per_group |
Column_to_Be_Grouped |
Pakistan |
Ehsan |
Saudi Arab |
Irshad |
Pakistan |
Ayesha |
India |
Ehsan |
Switzerland |
Ehsan |
Nigeria |
Ehsan |
Saudi Arabia |
Ayesha |
UK |
Ayesha |
Pakistan |
Zohan |
Afghanistan |
Zohan |
Iraq |
Zohan |
伊朗 |
Zohan |
美国 |
Zohan |
Netherland |
Irshad |
Switzerland |
Irshad |
India |
Irshad |
我想删除该小组中没有巴基斯坦的所有整个团体。例如,在我的数据框架中,我将从 column_to_be_grouped 中删除所有 irshad ,因为在所有irshad我都没有巴基斯坦,我所需的输出看起来像这样如下:
search_value_per_group |
column_to_be_grouped |
巴基斯坦 |
Ehsan |
巴基斯坦 |
Ayesha |
印度 |
Ehsan |
瑞士 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
I am having a trouble in deleting some records from a dataframe. If i groupby a certain column and check for each this column group in another column that for each group does it have any specific value in another column if that specific value does not exist delete that whole group from first column (The column upon which we have applied group by earlier). My data look like this:
search_value_per_group |
Column_to_Be_Grouped |
Pakistan |
Ehsan |
Saudi Arab |
Irshad |
Pakistan |
Ayesha |
India |
Ehsan |
Switzerland |
Ehsan |
Nigeria |
Ehsan |
Saudi Arabia |
Ayesha |
UK |
Ayesha |
Pakistan |
Zohan |
Afghanistan |
Zohan |
Iraq |
Zohan |
Iran |
Zohan |
USA |
Zohan |
Netherland |
Irshad |
Switzerland |
Irshad |
India |
Irshad |
I want to delete all that whole group which do not have Pakistan in that group. For example in my dataframe i will Delete All Irshad from Column_to_Be_Grouped because in all irshad i do not have Pakistan and my desired output will look like this as follows:
search_value_per_group |
Column_to_Be_Grouped |
Pakistan |
Ehsan |
Pakistan |
Ayesha |
India |
Ehsan |
Switzerland |
Ehsan |
Nigeria |
Ehsan |
Saudi Arabia |
Ayesha |
UK |
Ayesha |
Pakistan |
Zohan |
Afghanistan |
Zohan |
Iraq |
Zohan |
Iran |
Zohan |
USA |
Zohan |
发布评论
评论(2)
获取所有匹配的组并通过 用 a>:
Get all matched groups and filter them by
boolean indexing
withSeries.isin
:您可以使用 为
”
You can use
GroupBy.transform('any')
to generate a boolean Series for boolean indexing:output: