0

Which line of code is recommended to slice a Pandas dataframe based on condition? First method, based on .loc[]:

data_sorted_by_UDRS_laterality_Y_H_final = data_sorted_by_UDRS_laterality_Y_H.loc[(data_sorted_by_UDRS_laterality_Y_H['ECHELLE_H/Y_T0'] < 3) & (data_sorted_by_UDRS_laterality_Y_H['LATERALITY_BASED_ON_UPDRS'] != 'BILAT')]

Second method :

data_sorted_by_UDRS_laterality_Y_H_other = data_sorted_by_UDRS_laterality_Y_H[(data_sorted_by_UDRS_laterality_Y_H['LATERALITY_BASED_ON_UPDRS'] != 'BILAT') &  (data_sorted_by_UDRS_laterality_Y_H['ECHELLE_H/Y_T0'] < 3)]

Is there another way to do this conditional slicing easily within Pandas?

user1363251
  • 421
  • 1
  • 11
  • 24
  • Already discussed here: https://stackoverflow.com/questions/48409128/what-is-the-difference-between-using-loc-and-using-just-square-brackets-to-filte – FloLie Jul 05 '21 at 09:49
  • Does this answer your question? [What is the difference between using loc and using just square brackets to filter for columns in Pandas/Python?](https://stackoverflow.com/questions/48409128/what-is-the-difference-between-using-loc-and-using-just-square-brackets-to-filte) – FloLie Jul 05 '21 at 09:50
  • As a third option you could also use [`df.query()`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.query.html) to express this condition: `data_sorted_by_UDRS_laterality_Y_H.query('"LATERALITY_BASED_ON_UPDRS" != "BILAT" and "ECHELLE_H/Y_T0" < 3` – maow Jul 05 '21 at 09:53

0 Answers0