1
type     name     balance                                         
Assets   John     4.000         
Debt     John     0.359   
Assets   Anna     1.5
Debt     Anna     8
Assets   Bob      3.2
Debt     Jim      5 

code

df = pd.DataFrame.from_dict(data['info']).set_index(['type','name'])

return df.loc[df.balance > 2]

out put

                 balance 
type     name                                                     
Assets   John     4.000
Debt     Anna     8   
Assets   Bob      3.2
Debt     Jim      5 

i want out put like this if assets over 2

                 balance 
type     name                                                     
Assets   John     4.000   
Assets   Bob      3.2
Umar.H
  • 22,559
  • 7
  • 39
  • 74
Ravisut
  • 39
  • 8
  • Does this answer your question? [Pandas: How do I assign values based on multiple conditions for existing columns?](https://stackoverflow.com/questions/30631841/pandas-how-do-i-assign-values-based-on-multiple-conditions-for-existing-columns) – Waseem Randhawa Jun 27 '20 at 18:54

2 Answers2

3

IIUC, you can use df.filter along it's index 0 then chain .loc

df.filter(like='Assets',axis=0).loc[df.balance > 2]

             balance
type   name         
Assets John      4.0
       Bob       3.2

you can also use isin and specify the level.

df[(df.index.isin(['Assets'],level=0)) & (df.balance > 2)]


             balance
type   name         
Assets John      4.0
       Bob       3.2
Umar.H
  • 22,559
  • 7
  • 39
  • 74
0

You can use two conditions to first filter by column type and then by balance

df = df[df["type"] == "Assets"].loc[df["balance"] > 2]

Output:

    type    name    balance
0   Assets  John    4.0
4   Assets  Bob     3.2
Ozzy08
  • 155
  • 1
  • 9