Search code examples
pythonpandascomparison

Different behavior with the same logic applied in pandas dataframe


I need to filter a column of strings types to only return values ​​that are equal to 'NORMAL'. Applying the following codes returned different dataframes:

1 - df = df[(df['column_name'] == 'NORMAL')]

2 - df = df.drop(df[(df['column_name'] != 'NORMAL'].index)

3 - df = df.drop(df[(~df['column_name'].str.contains('NORMAL'))].index)

The resulting dataframe at 2 and 3 are equal but different from 1. The expected dataframe is made by example 1.

Am I missing something or is there a logical difference between the codes ?


Solution

  • As commented by @mozway, the difference in the return between the 3 example codes was in the index, when resetting the index the problem was solved.