我正在尝试编写一个脚本,使用dask从CSV中删除信息。我有一个从csv创建的dask 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
我有另一个csv与散列_客户,我需要从这个文件中删除。因此,如果此文件中的散列客户在客户订单中,我需要从行中删除firstname、lastname和email,同时保留其余内容,如下所示:
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
我当前的脚本如下所示:
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)]
但这是删除整行,现在我需要保留该行,只删除该行中的name和email元素。如何使用pandas从行中删除元素
我想得到一个相当于熊猫的dask:
df_cust_out.loc[df_in['hashed_eater_uuid'].isin(cust_opt_out_id),['firstname','lastname', 'email']]=np.nan
我建议查看Dataframe.where或Series.where方法:
https://docs.dask.org/en/latest/dataframe-api.html#dask.dataframe.DataFrame.where
相关问题 更多 >
编程相关推荐