0

First of all I am importing a csv file data to pandas

Now I have the following data

Schooltyp Name ParentName ParentAddress
Public Tom John Nanostreet
Private Bill Sally NaN
Public Ron Tony Burystreet
Public Danny Nate NaN
Private Stewart Ben PringleStreet

I need to remove data
where Schooltyp = Public and ParentAddress is null.

I tried different solutions. This is the latest solution I have currently used which results in an error due to the particular condition I chose (data['ParentAddress'].isnull)

data = pd.read_csv("dataload.csv", sep = ';',  error_bad_lines=False )

indexnames = data[(data['Schooltype']=='Public') & (data['ParentAddress'].isnull)].indexes
data.drop(indexnames, inplace = True)

data.to_csv('finaldata.csv', sep=';', index=False)

Am I using the right approach, is there a better way in doing this?

Tito
  • 601
  • 8
  • 23
  • Does this answer your question? [How to delete rows from a pandas DataFrame based on a conditional expression](https://stackoverflow.com/questions/13851535/how-to-delete-rows-from-a-pandas-dataframe-based-on-a-conditional-expression) – Vladimir Fokow Sep 01 '22 at 22:15
  • @VladimirFokow it didnt work earlier and suddenly now it works! However your approach works to and I did change the question in regards to the answer but I am not too experienced in Stackoverflow to know whether its a question worth keepin or not! I voted with you since you seem to be more advanced than I am here. – Tito Sep 01 '22 at 23:07

1 Answers1

1

To remove all rows where Schooltyp is "Public" and ParentAddress is null:

should_be_removed = (df['Schooltyp'] == 'Public') & df['ParentAddress'].isna()

df.loc[~ should_be_removed]

Result:

  Schooltyp     Name ParentName  ParentAddress
0    Public      Tom       John     Nanostreet
1   Private     Bill      Sally            NaN
2    Public      Ron       Tony     Burystreet
4   Private  Stewart        Ben  PringleStreet

Notes:

.ne() is equivalent to !=, just less typing.

There is also a method .eq() which is the same as ==.

To invert a condition, you can put ~ before it.

Vladimir Fokow
  • 3,728
  • 2
  • 5
  • 27