简体   繁体   English

如何使用仅包含数字的特定列删除Pandas Dataframe中的行?

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

Lets say I have this DF: 让我们说我有这个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. 我希望能够以去除 DF当所有行IGName只是数字。

Like how in this example, row 3 is all numbers. 就像在这个例子中,第3行是所有数字。 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. 检查Series / Index中每个字符串中的所有字符是否都是数字。 Equivalent to str.isnumeric(). 相当于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): 请注意,此解决方案假定列'IGName'的类型为字符串,否则您需要将其强制转换为字符串,执行类似(如@RafaelC所述):

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

Use df[...] : 使用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: 如果IGName有整数,请执行以下操作:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何创建将接受 pandas dataframe 的 function 并删除包含特定值的行? - How do I create a function that will accept a pandas dataframe and remove rows containing a specific value? 如果字符串具有“仅数字”,则从 pandas dataframe 中删除行 - Remove rows from pandas dataframe if string has 'only numbers' 从 Pandas 数据框中选择特定列包含数字的行 - Select rows from Pandas dataframe where a specific column contains numbers 将 Pandas Dataframe 的列(包含负数和正数)除以特定数字,并在 python 中创建一个新列? - Divide a column(containing both negative numbers and positive numbers) of a Pandas Dataframe with a specific number and create a new column in python? 使用pandas删除数据框中的特定行 - remove specific rows in dataframe with pandas 熊猫:仅当特定列中的值以开头时,才选择数据框行 - Pandas: select dataframe rows only if the values in a specific column start with 如何从Pandas数据框中删除带有特定缺失标签的行? - How to remove rows with specific missing tags from a Pandas dataframe? 如何根据列条目从熊猫数据框中删除随机行? - How to remove random rows from pandas dataframe based on column entry? 将 Pandas Dataframe 中的行与如何处理每一列的特定规则合并 - Merge rows in Pandas Dataframe with specific rules on how to handle each column 如何删除带有条件的 Pandas DataFrame 行以保留特定列值 - How to drop Pandas DataFrame rows with condition to keep specific column value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM