Python - 根据条件从 groupby 之后的 pandas.core.series.Series 中删除一行

Python - delete a row based on condition from a pandas.core.series.Series after groupby

按 2 列 casearea

分组后,我有这个 pandas.core.series.Series
case area
A 1 2494
2 2323
B 1 59243
2 27125
3 14

我只想保留 case A 中的区域,这意味着结果应该是这样的:

case area
A 1 2494
2 2323
B 1 59243
2 27125

我试过这段代码:

a = df['B'][~df['B'].index.isin(df['A'].index)].index
df['B'].drop(a)

成功了,输出是:

但它并没有把它放到数据框中,它还是一样。

当我给drop的结果赋值时,所有的值都变成了NaN

df['B'] = df['B'].drop(a)

我该怎么办?

分组后可以掉线,这里是一种方式

import pandas
import numpy as np

np.random.seed(1)

ungroup_df = pd.DataFrame({
    'case':[
        'A','A','A','A','A','A',
        'A','A','A','A','A','A',
        'B','B','B','B','B','B',
        'B','B','B','B','B','B',
    ],
    'area':[
        1,2,1,2,1,2,
        1,2,1,2,1,2,
        1,2,3,1,2,3,
        1,2,3,1,2,3,
    ],
    'value': np.random.random(24),
})

df = ungroup_df.groupby(['case','area'])['value'].sum()
print(df)

#index into the multi-index to just the 'A' areas
#the ":" is saying any value at the first level (A or B)
#then the df.loc['A'].index is filtering to second level of index (area) that match A's
filt_df = df.loc[:,df.loc['A'].index]
print(filt_df)

测试 df:

case  area
A     1       1.566114
      2       2.684593
B     1       1.983568
      2       1.806948
      3       2.079145
Name: value, dtype: float64

放下后的输出

case  area
A     1       1.566114
      2       2.684593
B     1       1.983568
      2       1.806948
Name: value, dtype: float64