1

I want to get result

df3=df.groupby(['Region']).apply(lambda x: x[x['Region'].isin(["North", "East"])]['Sales'].sum()).reset_index(name='sum')
      Region    sum
0   East    455.0
1   North   665.0
2   South   0.0
3   West    0.0

I want to do drop rows with value = 0 or another conditions

   Region   sum
0   East    455.0
1   North   665.0
sophocles
  • 13,593
  • 3
  • 14
  • 33
월봉리
  • 11
  • 2
  • i'm pretty sure you can find online how to drop rows where a column value equals to something – sophocles Dec 11 '22 at 12:23
  • Minial and reproducible example are needed to help you. https://stackoverflow.com/help/minimal-reproducible-example – Panda Kim Dec 11 '22 at 12:28

2 Answers2

0
You can use df.loc

df[1]!=0 -> True/False filter

df.loc[df[1]!=0] # Apply the filter
df=pd.DataFrame([['East',  455.0],
['North',  665.0],
['South',  0.0],
['West',  0.0]])
df
Out[11]: 
       0      1
0   East  455.0
1  North  665.0
2  South    0.0
3   West    0.0
df.loc[df[1]!=0]
Out[12]: 
       0      1
0   East  455.0
1  North  665.0


Answer to the comment: 
df.rename(columns={0:'region', 1:'sum'}).assign(**{'sum':lambda p:[q if q !=0 else pd.NA for q in p['sum']] }).dropna() (I am not sure if I understood it. Do you mean that?)
Hans
  • 148
  • 2
  • 7
0

Using df.loc is the easiest method it comes to mind

filtered_df = df3.loc[df3["sum"] != 0]
alec_djinn
  • 10,104
  • 8
  • 46
  • 71