如果该组删除该组不存在

发布于 2025-02-13 15:22:20 字数 1811 浏览 1 评论 0 原文

我在删除数据框的一些记录方面遇到了麻烦。如果我将某个列分组并检查另一列中的每个列组,而对于每个组都有任何特定值,则如果该特定值不存在,则从第一列中删除了整个组(我们所在的列早期应用组)。 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

弃爱 2025-02-20 15:22:21

获取所有匹配的组并通过 a>:

groups = df.loc[df['search_value_per_group'].eq('Pakistan'),'Column_to_Be_Grouped']
df1 = df[df['Column_to_Be_Grouped'].isin(groups)]

print (df1)
   search_value_per_group Column_to_Be_Grouped
0                Pakistan                Ehsan
2                Pakistan               Ayesha
3                   India                Ehsan
4             Switzerland                Ehsan
5                 Nigeria                Ehsan
6            Saudi Arabia               Ayesha
7                      UK               Ayesha
8                Pakistan                Zohan
9             Afghanistan                Zohan
10                   Iraq                Zohan
11                   Iran                Zohan
12                    USA                Zohan

Get all matched groups and filter them by boolean indexing with Series.isin:

groups = df.loc[df['search_value_per_group'].eq('Pakistan'),'Column_to_Be_Grouped']
df1 = df[df['Column_to_Be_Grouped'].isin(groups)]

print (df1)
   search_value_per_group Column_to_Be_Grouped
0                Pakistan                Ehsan
2                Pakistan               Ayesha
3                   India                Ehsan
4             Switzerland                Ehsan
5                 Nigeria                Ehsan
6            Saudi Arabia               Ayesha
7                      UK               Ayesha
8                Pakistan                Zohan
9             Afghanistan                Zohan
10                   Iraq                Zohan
11                   Iran                Zohan
12                    USA                Zohan
倒数 2025-02-20 15:22:21

您可以使用

out = df[df['search_value_per_group'].eq('Pakistan')
         .groupby(df['Column_to_Be_Grouped']).transform('any')]

   search_value_per_group Column_to_Be_Grouped
0                Pakistan                Ehsan
2                Pakistan               Ayesha
3                   India                Ehsan
4             Switzerland                Ehsan
5                 Nigeria                Ehsan
6            Saudi Arabia               Ayesha
7                      UK               Ayesha
8                Pakistan                Zohan
9             Afghanistan                Zohan
10                   Iraq                Zohan
11                   Iran                Zohan
12                    USA                Zohan

You can use GroupBy.transform('any') to generate a boolean Series for boolean indexing:

out = df[df['search_value_per_group'].eq('Pakistan')
         .groupby(df['Column_to_Be_Grouped']).transform('any')]

output:

   search_value_per_group Column_to_Be_Grouped
0                Pakistan                Ehsan
2                Pakistan               Ayesha
3                   India                Ehsan
4             Switzerland                Ehsan
5                 Nigeria                Ehsan
6            Saudi Arabia               Ayesha
7                      UK               Ayesha
8                Pakistan                Zohan
9             Afghanistan                Zohan
10                   Iraq                Zohan
11                   Iran                Zohan
12                    USA                Zohan
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文