Reputation: 305
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.
Upvotes: 1
Views: 3038
Reputation: 10624
The following should work:
df['col3']=df['col2'].apply(lambda x: 'No dot' if int(x)==x else 'Has dot')
Upvotes: 4
Reputation: 862511
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
Upvotes: 3