1

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?

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

3 Comments

thanks, upvoted. So 31 comes under float as well. It isn't integer?
Oh, wow, I looked at your second solution. So, if all are strings, we cannot segregate them based on float or string datatype?
@TheGreat - Then need your solution, a bit improved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.