'How to do the following loop operations on dataframes in a single line in pandas

I have to delete rows of a database according to certain conditions.

for index, row in df_A.iterrows():
   if name not in row["Name"].lower():
     df_A.drop(index, inplace= True)


for index, row in df_B.iterrows():
   if address != row["address"].split(":")[1]:
      df_B.drop(index, inplace= True)

for index, row in df_C.iterrows():
   name_given = name_dict[row["id"]]
   if name_given != name:
      df_C.drop(index, inplace= True)

The above code is working fine. But is there any shortcut way of doing these operations in pandas that do not use iterrows?



Solution 1:[1]

Use:

df_A[df_A['name'].str.lower().str.contains(name)]
df_B[df_B['address'].str.split(':').str[1].eq(address)]
df_C[df_C['id'].map(name_dict).eq(name)]

Solution 2:[2]

As u did not share sample data, could not test on your dataset but following worked for own dataset.

This following lines could replace of your first blocks of your code out of three without using 'iterrows'

df_A = df_A.fillna('0') #None/NAN cause error for 'series.str.contains' operation
df_A = df_A.drop(df_A[df_A['Name'].str.lower().str.contains(name)].index)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ansev
Solution 2