Count String Values in a Numeric Column Pandas

David 54321

I have a dataframe:

    Name    Hours_Worked
1   James   3
2   Sam     2.5
3   Billy   T
4   Sarah   A
5   Felix   5

1st how do I count the number of rows in which I have non-numeric values?

2nd how do I filter to identify the rows that contain non-numeric values?

jezrael

Use to_numeric with errors='coerce' for convert non numeric to NaNs and create mask by isna:

mask = pd.to_numeric(df['Hours_Worked'], errors='coerce').isna()
#oldier pandas versions
#mask = pd.to_numeric(df['Hours_Worked'], errors='coerce').isnull()    

Then count Trues values by sum:

a = mask.sum()
print (a)
2

And filter by boolean indexing:

df1 = df[mask]
print (df1)
    Name Hours_Worked
3  Billy            T
4  Sarah            A

Detail:

print (mask)
1    False
2    False
3     True
4     True
5    False
Name: Hours_Worked, dtype: bool

Another way for check numeric:

def check_num(x):
    try:
        float(x)
        return False        
    except ValueError:
        return True

mask = df['Hours_Worked'].apply(check_num)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Count frequency of values in pandas DataFrame column

Count rows that match string and numeric with pandas

Frequency count of values in a column of a pandas DataFrame

Pandas - count distinct values per column

Pandas - Count and get unique occurrences of string values from a column

pandas: split string, and count values?

Count positive values in column pandas

Pandas : Replace string column values

How to count number of characters in string for the column values and group rows by count of those as a result using pandas?

Combining numeric column values in large pandas DataFrame for duplicate rows without combining string

How to convert pandas data frame string values to numeric values

Pandas groupby column A, then group together consecutive numeric values in column B

Complete string values in a column guided by numeric values in another column

Pandas group by column and count values

Pandas - extract numeric values from string column using replace + regex

Pandas: count some values in a column

Python Pandas: groupby 3 dataframes with string, numeric and NaN values to a new dataframe using a common column

Pandas count sequence of negative values in column

Convert specific string values to specific numeric values in Pandas

How to count not null values on pandas column then agregating

Calculate count of a numeric column into new columns Pandas DataFrame

Convert column where values type are string to numeric

Extract only numeric values from String column

Evaluate Numeric list in pandas column as string list

sort and count values in a column DataFrame (Python Pandas)

pandas count consecutive values in a column

Group and take count by expanding values in column Pandas

Filter on a pandas string column as numeric without creating a new column

sub count of column values after group by pandas