我正在尝试编写一个脚本来清除csvs中的信息。我有一个从csv创建的pandas df,如下所示:
CUSTOMER ORDERS
hashed_customer firstname lastname email order_id status timestamp
0 eater 1_uuid 1_firstname 1_lastname 1_email 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 4_firstname 4_lastname 4_email 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
我还有另一个带有hashed_customers的csv,需要从该文件中清除。因此,如果此文件中的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)]
但是这将删除整个行,现在我需要保留该行,只删除该行中的名称和电子邮件元素。如何使用熊猫从一排放元素?
答案 0 :(得分:1)
让我们这样做
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