Nic*_*esh 2 python dataframe pandas
这是我的问题。
我知道如何基于 RegEx 创建一个布尔列,如下所示:
df['New Column'] = df.columnA.str.match(regex)
在此示例中,“新列”将包含 True 或 False 值。
但是,如果我想使用一个条件来说明“如果我的 RegEx 返回 true,则推送“这个”值,如果它返回 False,则推送“那个”值,我该怎么做。
感谢您的帮助 :)
您可以使用where()NumPy 中的函数:
df['New Column'] = np.where(df.columnA.str.match(regex), "this", "that")
您可以使用其他列名而不是标量:
df['New Column'] = np.where(df.columnA.str.match(regex), df.columnB, df.columnC)
| 归档时间: | 
 | 
| 查看次数: | 6584 次 | 
| 最近记录: |