python-有条件地写入xlsx

伙计们,

我目前正在使用庞大的Excel工作表,python 3.7.1和pandas 0.23.4.
我的任务是根据条件匹配写入单元格.像这样:

val = lincoln@gmx.net
if val in Cell_3A:
    write something to Cell_3B

一个完整的例子,让我们说以下是我的数据框:

    Email               Protection
1   lincoln@gmail.net
2   obama@gmail.net
3   trump@gmail.net
4   franklin@gmail.net

我知道要写下来,除第3行中的电子邮件外,所有电子邮件均受保护.因此,完成的数据框应如下所示:

    Email               Protection
1   lincoln@gmail.net   on
2   obama@gmail.net     on
3   trump@gmail.net     off
4   franklin@gmail.net  on

我该如何实现?

解决方法:

过滤“电子邮件”不是“ trump@gmail.net”的“保护”列,然后将其分配为“开启”,反之亦然.

df.loc[df['Email']!='trump@gmail.net', 'Protection']='on'
df.loc[df['Email']=='trump@gmail.net', 'Protection']='off'

使用np.where:

df['Protection'] = np.where((df['Email']!='trump@gmail.net'),'on','off')

要么:

df['Protection'] = np.where((df['Email']=='trump@gmail.net'),'off','on')

相关文章

转载:一文讲述Pandas库的数据读取、数据获取、数据拼接、数...
Pandas是一个开源的第三方Python库,从Numpy和Matplotlib的基...
整体流程登录天池在线编程环境导入pandas和xrld操作EXCEL文件...
 一、numpy小结             二、pandas2.1为...
1、时间偏移DateOffset对象DateOffset类似于时间差Timedelta...
1、pandas内置样式空值高亮highlight_null最大最小值高亮背景...