Identifying only numeric values from a column in a Data Frame- Python

Harish reddy

I Want a Separate column which returns "Yes" if the column "ID" contains all numeric values and 'No' if it contains alphabets or alphanumeric values.

ID      Result
3965      Yes
wyq8      No
RO_123    No
CMD_      No
2976      Yes
Ch3steR

You can use pd.Series.str.isnumeric here.

df['Result'] = np.where(df['ID'].str.isnumeric(), 'YES', 'NO')

       ID Result
0    3965    YES
1    wyq8     NO
2  RO_123     NO
3    CMD_     NO
4    2976    YES

There's a caveat with using isnumeric it doesn't identify float numbers.

test = pd.Series(["9.0", "9"])
test.str.isnumeric()

0    False
1     True
dtype: bool

If you strictly mark YES for int then use isnumeric else you can use pd.Series.str.fullmatch(available from version 1.1.0) here.

df['Result'] = np.where(df['ID'].str.fullmatch(r"\d+|\d+\.\d+", 'YES', 'NO')

For version <1.1.0 you use re.fullmatch

numeric_pat = re.compile(r"\d+|\d+\.\d+")
def numeric(val):
    match = numeric_pat.fullmatch(val)
    if match: return 'YES'
    else: return 'NO'

df['Result'] = df['ID'].apply(numeric)

Or we can use pd.to_numeric with boolean masking using pd.Series.isna

m = pd.to_numeric(df['ID'], errors='coerce').isna()
df['Result'] = np.where(m, 'NO', 'YES')

With errors parameter set to 'coerce' values which cannot be turned into numeic value will set to Nan.

test = pd.Series(['3965', 'wyq8', 'RO_123', 'CMD_', '2976'])
pd.to_numeric(test)

0    3965.0
1       NaN
2       NaN
3       NaN
4    2976.0
Name: ID, dtype: float64

Or you can build a custom function

def numeric(val):
    try:
        float(val)     # Using just `float` would suffice as int can be 
        return 'YES'   # converted to `float` so both `int`
                       # and `float` wouldnot raise any error
    except ValueError:
        return 'NO'

df['Result'] = df['ID'].apply(numeric)

Note: float handles scientic notation too, float("1e6") -> 1000000.0.

test = pd.Series(['1e6', '1', 'a 10', '1E6'])
test.apply(numeric)

0    YES
1    YES
2     NO
3    YES
dtype: object

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Identifying rows in data.frame with only NA values in R

From Dev

extract only numeric columns from data frame

From Dev

extract only numeric columns from data frame

From Dev

How to convert values of a pandas data frame column to a numeric representation based on the number of unique values, all at once in Python?

From Dev

Return only numeric values from column - Postgresql

From Dev

Return only numeric values from column - Postgresql

From Dev

How to predict values of column in new Python data frame using info from the old data frame

From Dev

matching the ids from different data frame and arranging the column values in to a single data frame in python

From Dev

How to convert data.frame column from Factor to numeric

From Dev

select numeric columns and one column specified by name from data frame

From Dev

How to extract the high numeric values from data frame in R

From Dev

Mean and SD from a data frame of non numeric values

From Dev

Python filtering for numeric and string in a single data frame column

From Dev

MySQL - Select only numeric values from varchar column

From Dev

Converting a list to multiple data frames, to be able to convert only a column of each data frame to numeric data

From Dev

Create new columns in a data frame based on an existing numeric column, a list of strings as column names and a list of tuples as values

From Dev

how to check if a column in a data frame contains only a specific set of values

From Dev

Create New Data Frame with Column Names from Unique Values in another Data Frame and Corresponding Values Assigned to Column

From Dev

Data frame with unique values from other data frame(pandas, python)

From Dev

R: Identifying Data Frame Rows Connected By Shared Values In Two Columns

From Dev

Dividing values in a column of a data frame by values from a different data frame when row values match

From Dev

Python Data frame: Create New Column that Conditionally Concatenates String Values from 1 or 3 Other Columns

From Dev

Converting a column in a data.frame to numeric type to calculate mean values in R

From Dev

Replacing values in a column of a data.frame with values from another data.frame

From Dev

Identifying and selecting single elements from a huge data frame in R

From Dev

select only columns containing values > 5 from a data frame

From Dev

Making the column of the DataTable to accept only numeric values

From Dev

Create new data frame column from past values

From Dev

How to take values from a list to a column of the data frame in R

Related Related

  1. 1

    Identifying rows in data.frame with only NA values in R

  2. 2

    extract only numeric columns from data frame

  3. 3

    extract only numeric columns from data frame

  4. 4

    How to convert values of a pandas data frame column to a numeric representation based on the number of unique values, all at once in Python?

  5. 5

    Return only numeric values from column - Postgresql

  6. 6

    Return only numeric values from column - Postgresql

  7. 7

    How to predict values of column in new Python data frame using info from the old data frame

  8. 8

    matching the ids from different data frame and arranging the column values in to a single data frame in python

  9. 9

    How to convert data.frame column from Factor to numeric

  10. 10

    select numeric columns and one column specified by name from data frame

  11. 11

    How to extract the high numeric values from data frame in R

  12. 12

    Mean and SD from a data frame of non numeric values

  13. 13

    Python filtering for numeric and string in a single data frame column

  14. 14

    MySQL - Select only numeric values from varchar column

  15. 15

    Converting a list to multiple data frames, to be able to convert only a column of each data frame to numeric data

  16. 16

    Create new columns in a data frame based on an existing numeric column, a list of strings as column names and a list of tuples as values

  17. 17

    how to check if a column in a data frame contains only a specific set of values

  18. 18

    Create New Data Frame with Column Names from Unique Values in another Data Frame and Corresponding Values Assigned to Column

  19. 19

    Data frame with unique values from other data frame(pandas, python)

  20. 20

    R: Identifying Data Frame Rows Connected By Shared Values In Two Columns

  21. 21

    Dividing values in a column of a data frame by values from a different data frame when row values match

  22. 22

    Python Data frame: Create New Column that Conditionally Concatenates String Values from 1 or 3 Other Columns

  23. 23

    Converting a column in a data.frame to numeric type to calculate mean values in R

  24. 24

    Replacing values in a column of a data.frame with values from another data.frame

  25. 25

    Identifying and selecting single elements from a huge data frame in R

  26. 26

    select only columns containing values > 5 from a data frame

  27. 27

    Making the column of the DataTable to accept only numeric values

  28. 28

    Create new data frame column from past values

  29. 29

    How to take values from a list to a column of the data frame in R

HotTag

Archive