1

How do I take data frame, like the following:

      col1    col2
row0   abc      3
row1   bcd     2.4

And produce a dataframe with new column which value based on col2, is number has dot in it or not, like the following:

      col1    col2    col3
row0   abc      3     No dot
row1   bcd     2.4    Has dot

Any help is appreciated.

2 Answers 2

4

The following should work:

df['col3']=df['col2'].apply(lambda x: 'No dot' if int(x)==x else 'Has dot')
Sign up to request clarification or add additional context in comments.

Comments

3

Use numpy.where with Series.str.contains, because . is special regex character escape it by \:

df['col3'] = np.where(df['col2'].astype(str).str.contains('\.'), 'Has dot', 'No dot')

Or use regex=False parameter:

df['col3'] = np.where(df['col2'].astype(str).str.contains('.', regex=False), 
                      'Has dot', 'No dot')

print (df)

     col1 col2     col3
row0  abc    3   No dot
row1  bcd  2.4  Has dot

Comments

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.