Home > Software engineering >  How to assign a data frame to a variable depending on two or more colums condition
How to assign a data frame to a variable depending on two or more colums condition

Time:05-30

Am not sure if my quest is well framed but here it is.

here in this code i read the csv file and assign it to the variable australia_analysis specifying that the location column should be AUS

this is the output when i print it

How can i assign the dataframe to the variable under two conditions that the location should be AUS and the subject should be MEASLES so that i only get rows that satisfy the condition

CodePudding user response:

You can filter based on multiple columns using this code

import pandas as pd
df = pd.DataFrame([{"a":1,"b":1},{"a":1,"b":2},{"a":2,"b":2},{"a":2,"b":1}])
df2 = df[(df["a"] ==1) & (df["b"]==2)]

CodePudding user response:

You can combine multiple conditions with &:

loaded_data.loc[(loaded_data['LOCATION'] == 'AUS') & (loaded_data['SUBJECT'] == 'MEASLES')]
  • Related