为了账号安全,请及时绑定邮箱和手机立即绑定

Python Pandas 从行中删除

Python Pandas 从行中删除

肥皂起泡泡 2022-11-29 15:26:43
我正在尝试编写一个脚本来从 csvs 中清除信息。我有一个从 csv 创建的 pandas df,如下所示:CUSTOMER ORDERS  hashed_customer      firstname    lastname    email   order_id    status          timestamp0      eater 1_uuid  1_firstname  1_lastname  1_email    12345    OPTED_IN     2020-05-14 20:45:151      eater 2_uuid  2_firstname  2_lastname  2_email    23456    OPTED_IN     2020-05-14 20:29:222      eater 3_uuid  3_firstname  3_lastname  3_email    34567    OPTED_IN     2020-05-14 19:31:553      eater 4_uuid  4_firstname  4_lastname  4_email    45678    OPTED_IN     2020-05-14 17:49:274      eater 5_uuid  5_firstname  5_lastname  5_email    56789    OPTED_IN     2020-05-14 16:22:33我有另一个 csv,其中包含我需要从该文件中清除的 hashed_customers。因此,如果此文件中的 hashed_customer 在 CUSTOMER ORDERS 中,我需要从行中删除名字、姓氏和电子邮件,同时保留其余部分,如下所示:CUSTOMER ORDERS      hashed_customer      firstname    lastname    email   order_id    status          timestamp    0      eater 1_uuid         NULL        NULL     NULL    12345    OPTED_IN     2020-05-14 20:45:15    1      eater 2_uuid  2_firstname  2_lastname  2_email    23456    OPTED_IN     2020-05-14 20:29:22    2      eater 3_uuid  3_firstname  3_lastname  3_email    34567    OPTED_IN     2020-05-14 19:31:55    3      eater 4_uuid         NULL        NULL     NULL    45678    OPTED_IN     2020-05-14 17:49:27    4      eater 5_uuid  5_firstname  5_lastname  5_email    56789    OPTED_IN     2020-05-14 16:22:33我当前的脚本如下所示:print('FIND ORDERS FROM OPT-OUT CUSTOMERS')cust_opt_out_order = []for index, row in df_in.iterrows():    if row.hashed_eater_uuid in cust_opt_out_id:        cust_opt_out_order.append(row.order_id)print('REMOVE OPT-OUT FROM OPT-IN FILE')df_cust_out = df_in[~df_in['hashed_eater_uuid'].isin(cust_opt_out_id)]但这是删除整行,现在我需要保留该行,只从该行中删除名称和电子邮件元素。如何使用熊猫从一行中删除元素?
查看完整描述

1 回答

?
萧十郎

TA贡献1815条经验 获得超12个赞

让我们做


df_cust_out = df_in.copy()

df_cust_out.loc[df_in['hashed_eater_uuid'].isin(cust_opt_out_id),['firstname','lastname', 'email']]=np.nan



查看完整回答
反对 回复 2022-11-29
  • 1 回答
  • 0 关注
  • 61 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信