Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何基于dataframe中的另一列值添加列?_Python_Python 3.x_Pandas_Dataframe - Fatal编程技术网

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')