简体   繁体   English

从 python 中的 csv 删除行

[英]delete row from csv in python

I have two different csv file(as success.csv and failure.csv) which contains same column value.我有两个不同的 csv 文件(如 success.csv 和 failure.csv),其中包含相同的列值。 I have to delete rows from success.csv if value(msisdn column value) found in error.csv file.如果在 error.csv 文件中找到值(msisdn 列值),我必须从 success.csv 中删除行。

Success.csv成功。csv

[root@ngpdn15 MSSuccess]# cat msLogSuccess.csv
MSISDN,REQUEST_ID,STATE1,STATE2,STAETE3,NOTIFICATION
22969000034,OFFLINE_Notification_10.10.46.95_e6444dbc-b7ef-41d6-9111-eaa384717b27,ENTERED,IN_PROGRESS,COMPLETED,Successfully send SMS to the subscriber
22969000035,OFFLINE_Notification_10.10.46.95_e6444dbc-b7ef-41d6-9111-eaa384717b27,ENTERED,IN_PROGRESS,COMPLETED,Successfully send SMS to the subscriber
22969000036,OFFLINE_Notification_10.10.46.95_e6444dbc-b7ef-41d6-9111-eaa384717b27,ENTERED,IN_PROGRESS,NA,NA
22969000037,OFFLINE_Notification_10.10.46.95_e6444dbc-b7ef-41d6-9111-eaa384717b27,ENTERED,NA,NA,NA
22969000038,OFFLINE_Notification_10.10.46.95_e6444dbc-b7ef-41d6-9111-eaa384717b27,ENTERED,IN_PROGRESS,NA,NA
22969000039,OFFLINE_Notification_10.10.46.95_e6444dbc-b7ef-41d6-9111-eaa384717b27,ENTERED,IN_PROGRESS,NA,NA

failure.csv故障。csv

MSISDN,REQUEST_ID,STATE1,STATE2,STAETE3,NOTIFICATION
22969000038,OFFLINE_Notification_10.10.46.95_e6444dbc-b7ef-41d6-9111-eaa384717b27,ENTERED,INPROGRESS,SMS_FAILED,Not send SMS to the subscriber
22969000039,OFFLINE_Notification_10.10.46.95_e6444dbc-b7ef-41d6-9111-eaa384717b27,ENTERED,IN_PROGRESS,FAILURE,Not send SMS to the subscriber

In above two file msisdn available in failure.csv are also available in Success.csv so i have to delete entries for these msisdn from success.csv file in python. In above two file msisdn available in failure.csv are also available in Success.csv so i have to delete entries for these msisdn from success.csv file in python. Please suggest.请建议。

I think that a good approach would be using pandas.我认为一个好的方法是使用 pandas。 You can load the files as pandas DataFrames and then do something like this:您可以将文件加载为 pandas DataFrames,然后执行以下操作:

import pandas as pd
source = pd.read_csv("./success.csv")
failure = pd.read_csv("./failure.csv")
success[~success.MSISDN.isin(failure.MSISDN)]

I hope this will help you.我希望这能帮到您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM