亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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條經驗 獲得超13個贊

讓我們做


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 關注
  • 93 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號