How to identify float/numbers in String data using pandas

I have a dataframe like as shown below

df = pd.DataFrame({'val': ['test','depat','23.1','25.0','31',np.nan]})

I would like to crete two new columns val_num and val_string

In val_num, I would like to store numeric/int values

In val_string, I would like to store string values

So, I tried the below

df['val_num'] = pd.to_numeric(df['val'],errors='coerce')
df['val_string'] = (df[pd.to_numeric(df['val'],errors='coerce').isna()])

Though the above works fine, is there any elegant function like to_numeric for identifying string objects using to_string?

Answer

is there any elegant function like to_numeric for identifying string objects using to_string

No, it not exist yet.

If values are mixed – it means there ais possible use isinstance method for test it:

df = pd.DataFrame({'val': ['test','depat',23.1,25.0,31,np.nan]})

df['num'] = df.loc[df['val'].apply(lambda x: isinstance(x, (float, int))), 'val']
df['str'] = df.loc[df['val'].apply(lambda x: isinstance(x, str)), 'val']
print (df)
     val   num    str
0   test   NaN   test
1  depat   NaN  depat
2   23.1  23.1    NaN
3   25.0  25.0    NaN
4     31    31    NaN
5    NaN   NaN    NaN

Unfortuantely in real life all data are strings, so need your solution – first convert to numeric and then processing:

df = pd.DataFrame({'val': ['test','depat','23.1','25.0','31',np.nan]})

df['num'] = df.loc[df['val'].apply(lambda x: isinstance(x, float)), 'val']
df['str'] = df.loc[df['val'].apply(lambda x: isinstance(x, str)), 'val']
print (df)
     val  num    str
0   test  NaN   test
1  depat  NaN  depat
2   23.1  NaN   23.1
3   25.0  NaN   25.0
4     31  NaN     31
5    NaN  NaN    NaN

df['num'] = pd.to_numeric(df['val'],errors='coerce')
df['vstring'] = df.loc[df['num'].isna(), 'val']
print (df)
     val   num vstring
0   test   NaN    test
1  depat   NaN   depat
2   23.1  23.1     NaN
3   25.0  25.0     NaN
4     31  31.0     NaN
5    NaN   NaN     NaN