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