简体   繁体   English

从 Pandas 数据框中选择特定列包含数字的行

[英]Select rows from Pandas dataframe where a specific column contains numbers

I have a data frame where a column (column B) can contain a letters, a number or nothing at all.我有一个数据框,其中一列(B 列)可以包含字母、数字或根本不包含任何内容。 Lets say the data frame is:假设数据框是:

A   B    C
1   2    Dog
3   C    Bird
30  nan  Cat
11  4.1  Wolf

And I want to get rows conditionally, based on whether there is a number in column B:我想根据 B 列中是否有数字来有条件地获取行:

A   B    C
1   2    Dog
11  4.1  Wolf

I have found that I can limit the dataframe to only rows that contain values by entering df.loc[df["B"].notnull()] .我发现我可以通过输入df.loc[df["B"].notnull()]将数据df.loc[df["B"].notnull()]限制为仅包含值的行。 What I'm trying to find out is whether or not there is an equivalent version of .notnull() that can select only rows where column B contains a number?我想知道是否有一个等效版本的.notnull()只能选择列 B 包含数字的行?

To find integers and decimal numbers, define a function that will take a string as an input, attempt to convert a value to a floating point number (which will succeed if you have an integer or a floating point number), and will handle possible errors: a ValueError is raised if you pass it a string that can't be converted to a floating point number, and a TypeError is raised if a null value is passed to float() , so handle these two exceptions:要查找整数和十进制数,请定义一个函数,该函数将字符串作为输入,尝试将值转换为浮点数(如果您有整数或浮点数,则会成功),并将处理可能的错误:如果传递给它的字符串无法转换为浮点数,则会引发ValueError ,如果将空值传递给float() ,则会引发TypeError ,因此请处理这两个异常:

def safe_float_convert(x):
    try:
        float(x)
        return True # numeric, success!
    except ValueError:
        return False # not numeric
    except TypeError:
        return False # null type

Now use map() to map the new function to column B of the dataframe, element-wise, and create a boolean mask:现在使用map()将新函数按元素映射到数据框的 B 列,并创建一个布尔掩码:

mask = df['B'].map(safe_float_convert)

and use the .loc[] function, passing in the boolean mask:并使用.loc[]函数,传.loc[]掩码:

numeric_df = df.loc[mask]

暂无
暂无

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

相关问题 Pandas dataframe - 选择一列的值包含字符串,另一列的值以特定字符串开头的行 - Pandas dataframe - Select rows where one column's values contains a string and another column's values starts with specific strings Pandas 数据框选择列表列包含任何字符串列表的行 - Pandas dataframe select rows where a list-column contains any of a list of strings 最简洁的方法是 select 行,其中任何列包含 Pandas dataframe 中的字符串? - Most concise way to select rows where any column contains a string in Pandas dataframe? 返回 pandas dataframe 中列中的元组包含特定值的行 - Return rows in pandas dataframe where tuple in column contains a certain value 如何从熊猫数据框中删除行,其中任何列都包含我不想要的符号 - How to drop rows from a pandas dataframe where any column contains a symbol I don't want 熊猫-在ANY列中选择包含某个正则表达式的数据框的行 - Pandas - Select rows of a dataframe that contains a certain regex in ANY column 如何使用仅包含数字的特定列删除Pandas Dataframe中的行? - How to remove rows in a Pandas Dataframe with a specific column containing numbers only? 从 Pandas MultiIndex 数据框中选择特定列 - select specific column from pandas MultiIndex dataframe 从 Pandas 数据框中选择多行,其中一列包含一些作为 NaN 的值 - Select multiple rows from pandas data frame where one of column contains some values as NaN Python Pandas选择组,其中特定列包含零 - Python Pandas select group where a specific column contains zeroes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM