2

I am writing a simple Contact Book app in Python using pandas and I want to add a function of removing a contact selected by used from a book. How can I do this? I tried pd.drop() function many times with different parameters but it did not work.

import pandas as pd
import numpy as np
import re
import csv

print("""Hello, that is app for Contact Book!
Use phone number format as 0516677699 tho
If you want to add contact, press ("A")
If you want to remove contact press ("R")
""")
contacts = pd.DataFrame(columns=["Name", "Phone Number"], index=range(1, 11) )
print(contacts)
operation = input("Do you want to enter contact book ?  Press Y : ")

while operation == "Y":
    oper1 = input("""What do you want to do?
      A - add someone's contact
      L - leave contact book
      R - remove someone's contact
      """)
    if oper1 == "A" :
        oper2 = input('Type his name : ')
        oper3 = input('Type his number: ')
        raw = {'Name': oper2,
               'Phone Number': oper3}
        contacts = contacts.append(raw, ignore_index=True)
    if oper1 == 'L':
        break
    if oper1 == "R":
        contacts.drop([contacts['Name'] == oper2, contacts['Phone Number'] == oper3])
    print(contacts)
contacts_csv = []
Nino
  • 21
  • 3

2 Answers2

1

This is how you must construct the drop command:

contacts.drop(contacts[(contacts['Name'] == oper2) & (contacts['Phone Number'] == oper3)].index)

However you need to ask the user to input the Name and Phone again, for this particular 'if-else' statement. Here is the full solution:

if oper1 == "R" :
    oper2 = input('Type his name : ')
    oper3 = input('Type his number: ')
    contacts.drop(contacts[(contacts['Name'] == oper2) & (contacts['Phone Number'] == oper3)].index)
IoaTzimas
  • 10,538
  • 2
  • 13
  • 30
1

You should add some elif in your code to allow the different choices:

import pandas as pd
import numpy as np
import re
import csv

print("""Hello, that is app for Contact Book!
Use phone number format as 0516677699 tho
If you want to add contact, press ("A")
If you want to remove contact press ("R")
""")
contacts = pd.DataFrame(columns=["Name", "Phone Number"], index=range(1, 11) )
print(contacts)
operation = input("Do you want to enter contact book ?  Press Y : ")

while operation == "Y":
    oper1 = input("""What do you want to do?
      A - add someone's contact
      L - leave contact book
      R - remove someone's contact
      """)
    if oper1 == "A" :
        oper2 = input('Type his name : ')
        oper3 = input('Type his number: ')
        raw = {'Name': oper2,
               'Phone Number': oper3}
        contacts = contacts.append(raw, ignore_index=True)
    elif oper1 == 'L':
        break
    elif oper1 == "R":
        oper2 = input('Type his name : ')
        oper3 = input('Type his number: ')
        contacts.drop(contacts[(contacts['Name'] == oper2) & (contacts['Phone Number'] == oper3)].index)
    print(contacts)
contacts_csv = []