this code in pandas does not work. I want it to delete the row if the column contains any of the text/numbers provided Currently I can only get it to work if the cell matches the exact text being passed in my code .. as in it only deletes cells that say Fin* not Finance or Finly...
df2 = df[df.Team != 'Fin*']
Use pandas. DataFrame. drop() method to delete/remove rows with condition(s).
Dropping a row in pandas is achieved by using .drop () function. Lets see example of each. Drop Rows with Duplicate in pandas. Delete or Drop rows with condition in python pandas using drop () function. Drop rows by index / position in pandas.
This article demonstrates how to delete rows if a cell contains specific text in Excel. Say you have data set with names in Columns A, B, and C and want to select all the cells with the name John, then delete all rows containing those cells. 1. First, select the data set (A2:C6). Then, in the Ribbon, go to Home > Find & Select > Find. 2.
In this article we will learn how to remove the rows with special characters i.e; if a row contains any value which contains special characters like @, %, &, $, #, +, -, *, /, etc. then drop such row and modify the data. To drop such types of rows, first, we have to search rows having special characters per column and then drop.
Syntax of drop () function in pandas : DataFrame.drop (labels=None, axis=0, index=None, columns=None, level=None, inplace=False, errors=’raise’) labels: String or list of strings referring row. axis: int or string value, 0 ‘index’ for Rows and 1 ‘columns’ for Columns.
You can using startswith
df[~df.Team.str.startswith('Fin')]
Or
df[~df.Team.str.contains('Fin')]
import pandas as pd
df = pd.DataFrame(dict(A=[1,2,3,4], C=["abc","def","abcdef", "lmn"]))
df:
A C
0 1 abc
1 2 def
2 3 abcdef
3 4 lmn
df[df.C.str.contains("abc") == False]
OR as suggested by @RafaelC
df[~df.C.str.contains("abc")]
Output:
A C
1 2 def
3 4 lmn
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With