Python 如何基于dataframe中的另一列值添加列?
如何获取数据帧,如下所示:Python 如何基于dataframe中的另一列值添加列?,python,python-3.x,pandas,dataframe,Python,Python 3.x,Pandas,Dataframe,如何获取数据帧,如下所示: col1 col2 row0 abc 3 row1 bcd 2.4 col1 col2 col3 row0 abc 3 No dot row1 bcd 2.4 Has dot 并生成一个具有新列的数据帧,该列的值基于col2,即数字是否包含点,如下所示: col1 col2 row0 abc 3 row1 bcd
col1 col2
row0 abc 3
row1 bcd 2.4
col1 col2 col3
row0 abc 3 No dot
row1 bcd 2.4 Has dot
并生成一个具有新列的数据帧,该列的值基于col2,即数字是否包含点,如下所示:
col1 col2
row0 abc 3
row1 bcd 2.4
col1 col2 col3
row0 abc 3 No dot
row1 bcd 2.4 Has dot
非常感谢您的帮助。与一起使用,因为
是通过\
对其进行转义的特殊正则字符:
df['col3'] = np.where(df['col2'].astype(str).str.contains('\.'), 'Has dot', 'No dot')
或者使用regex=False
参数:
df['col3'] = np.where(df['col2'].astype(str).str.contains('.', regex=False),
'Has dot', 'No dot')
与一起使用,因为
是通过\
对其进行转义的特殊正则表达式字符:
df['col3'] = np.where(df['col2'].astype(str).str.contains('\.'), 'Has dot', 'No dot')
或者使用regex=False
参数:
df['col3'] = np.where(df['col2'].astype(str).str.contains('.', regex=False),
'Has dot', 'No dot')
以下方面应起作用:
df['col3']=df['col2'].apply(lambda x: 'No dot' if int(x)==x else 'Has dot')
以下方面应起作用:
df['col3']=df['col2'].apply(lambda x: 'No dot' if int(x)==x else 'Has dot')