Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: Delete Row if cell contains specific text

Tags:

python

pandas

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*']
like image 407
0004 Avatar asked Jul 05 '18 00:07

0004


People also ask

How do I delete a row based on conditions in pandas?

Use pandas. DataFrame. drop() method to delete/remove rows with condition(s).

How to delete or drop a row in Python pandas?

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.

How to delete rows if a cell contains specific text in Excel?

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.

How to remove the rows with special characters in Excel?

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.

What is the syntax of drop () function in pandas Dataframe?

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.


2 Answers

You can using startswith

df[~df.Team.str.startswith('Fin')]

Or

df[~df.Team.str.contains('Fin')]
like image 117
BENY Avatar answered Oct 21 '22 03:10

BENY


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
like image 22
min2bro Avatar answered Oct 21 '22 03:10

min2bro