I want to delete specific strings with regular expressions from the column Sorte
which I don’t want to have in my dataframe file_df
with the following code:
JavaScript
x
8
1
file_df = file_df[(file_df.Sorte != 'sonstige') & (file_df.Sorte != 'verauslagte Portokosten')
2
& (file_df.Sorte != 'erhaltenenzahlung Re vom')
3
& (file_df.Sorte != 'geleistetenzahlung aus Re-Nr')
4
& (file_df.Sorte != '^.*Holzkisten geliefert.*$')
5
& (file_df.Sorte != '^.*Infomaterialktionspakete.*$')
6
& (file_df.Sorte != '^.*Aloe Vera haben wir nicht im Sortiment.*$')
7
& (file_df.Sorte != '^.*Anzeigenvorlage Planten ut`norden.*$')]
8
But somehow when I execute this code these strings still are in the dataset and I can not figure out why. I wanted to chain this expression to not create so many copies.
Update
The code worked for some strings in the dataset, for others not.
Advertisement
Answer
I figured out another solution that worked out for me derived from the answer at https://stackoverflow.com/a/54410702/14553595, which is basically a combination of your suggestions in the comments:
JavaScript
1
16
16
1
file_df = file_df.loc[:,~(file_df.columns.str.contains('^.*Fracht.*$', case=False)
2
| file_df.columns.str.contains('^.*Angebotspaket.*$', case=False)
3
| file_df.columns.str.contains('^.*Werbe.*$', case=False)
4
| file_df.columns.str.contains('^.*Vita.Verde.*$', case=False)
5
| file_df.columns.str.contains('^.*zahlung.*$', case=False)
6
| file_df.columns.str.contains('^.*Europalette.*$', case=False)
7
| file_df.columns.str.contains('^.*Angebotspaket.*$', case=False)
8
| file_df.columns.str.contains('^.*Aufkleber fuer Saeule.*$', case=False)
9
| file_df.columns.str.contains('^.*Aufsetzer.*$', case=False)
10
| file_df.columns.str.contains('^.*Ausstellen der Pflanzen in die Beete pauschal.*$', case=False)
11
| file_df.columns.str.contains('^.*Ausstellungsflaeche.*$', case=False)
12
| file_df.columns.str.contains('^.*Auswaschen.*$', case=False)
13
| file_df.columns.str.contains('^.*Bild.*$', case=False)
14
| file_df.columns.str.contains('^.*etikette.*$', case=False)
15
)]
16