str.replace()删除python数据框中的列

时间:2019-07-19 04:53:12

标签: python-3.x pandas

我有一个包含2列的数据框。

id          data


135790075   job done, pay by card 4444-5555-6666-7777

我有25k这样的行,其中id是唯一的。接下来,我将每个卡号传递给Luhn支票,并掩盖通过支票的卡号。

data = pd.read_csv("sample.csv")
summ = data['summary']
creditcards = []
regex_match_index_list =[]
Validcardsfound = 0
regex_count = 0
for i in range(2):
    temp = re.findall(r'(\d\B(?:\d[ -]*?){13,16}\b)',str(values[i]))

    if temp:
        for each in temp:

            regex_count = regex_count + 1
            if doLuhn(str(each)) is True:


                creditcards.append(each)
                Validcardsfound = Validcardsfound + 1
                regex_match_index_list.append(i)
                #else:
                #    pass

            elif doLuhn(str(temp)) is False:
                pass




    else:
        pass
rows =[]


for each in regex_match_index_list:

    changed = data.iloc[each].str.replace(r'(\d\B(?:\d[ -]*?){13,16}\b)', r'(xxxx-xxxx-xxxx-xxxx')
   # print("Changed", changed)
    rows.append(changed)

当我尝试使用str.replace函数替换卡号时,我丢失了id列。替换后创建新的csv时,id字段为空。如果删除str.replace,则id字段不会消失。我在做什么错了?

1 个答案:

答案 0 :(得分:0)

我很确定,但是iloc[each]将返回整行。使用iloc[[each,0]]可能会为您提供所需的输出