简体   繁体   中英

How to remove rows in a Pandas Dataframe with a specific column containing numbers only?

Lets say I have this DF:

ID     IGName          Date_created
0     BananaMan         09/10/2018
1     Superman247       10/10/2009
2     123456789         08/03/2011
3     Nameless101       07/12/2012

I want to be able to remove all the rows in the DF where the IGName is only numbers.

Like how in this example, row 3 is all numbers. I want to be able to keep the names alphanumeric rows but not the rows with ONLY numerics.

I want the result to look like this:

ID     IGName          Date_created
0     BananaMan         09/10/2018
1     Superman247       10/10/2009
3     Nameless101       07/12/2012

You could do:

import pandas as pd


data = [[0, 'BananaMan', '09/10/2018'],
        [1, 'Superman247', '10/10/2009'],
        [2, '123456789', '08/03/2011'],
        [3, 'Nameless101', '07/12/2012']]

df = pd.DataFrame(data=data, columns=['ID', 'IGName', 'Date_created'])

df = df[~df['IGName'].str.isnumeric()]

print(df)

Output

   ID       IGName Date_created
0   0    BananaMan   09/10/2018
1   1  Superman247   10/10/2009
3   3  Nameless101   07/12/2012

From the documentation :

Check whether all characters in each string in the Series/Index are numeric. Equivalent to str.isnumeric().

Note that this solution assumes the column 'IGName' is of type string, otherwise you need to cast it to string, doing something like (as mentioned by @RafaelC):

df['IGName'] = df['IGName'].astype(str)

Use df[...] :

print(df[~df['IGName'].str.isnumeric()])

Or:

print(df[df['IGName'].str.contains(r'\D+')])

Both Output:

   ID       IGName Date_created
0   0    BananaMan   09/10/2018
1   1  Superman247   10/10/2009
3   3  Nameless101   07/12/2012

If IGName has integers do:

print(df[pd.to_numeric(df.IGName, errors='coerce').notnull()])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM