Question

PYTHON: I have a numpy array that holds a name and a phone number such as...

PYTHON:

I have a numpy array that holds a name and a phone number such as this one.

import numpy as np

import pandas as pd

LIST = np.array([("James", "Kirk", "512-375-5585"), ("Richard", "Branson, "1234567890"), ("James", "Bond", "34567")])

Then I stuff it into a data frame by doing this

df = pd.DataFrame(List, columns=('Firstname', 'Lastname', 'Phone'))

How would I go about changing 10 digit phone numbers such as 1234567890 into 123-456-7890 and discarding invalid ones such as 34567 so it reflects in the data frame using a regular expression? I have tried df.replace but I am getting errors. Note that I don't want to remove properly formatted phone numbers that are already inside the data frame like 512-375-5585 should stay in the data frame.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Don't have much idea about pandas and numpy but for the format of phone number you want has been done in this python code. I hope the logic I have used here may help you. I have used a text file for the recods 


f = open("record.txt","r")              #opening the fike record.txt in read mode

content_list =[]                        #storing the content of text in a list

for  line in f:
    for word in line.split():           #splittinng each line into words
        content_list.append(word)       #storing each word in the list

fmt = '{:<10} {:<10} {:<12} {:<12}'     #formating the display
print(fmt.format('Firstname','Lastname','Pin','Number'));

for x in range(0,len(content_list),4):
    pin = list()                        #each word of the list is a string
    number = list()                     #which is immutable so converting to list
    for j in content_list[x+2]:
        pin.append(j)
    if pin[5]!="-":                     #checking if the format of zip is correct
        pin.insert(5, "-")              #if not then modify
    else:
        pass
    for j in content_list[x + 3]:
        number.append(j)
    if number[3]=='-' and number[3]=='-':    #checking the format of phone number
        pass                                 #if correct then pass
    else:
        number.insert(3, "-")                #else modify
        number.insert(7, "-")
    pinstr=""
    numstr=""
    for j in pin:
          pinstr+=j                          #converting ti string so that we can print
    for j in number:
         numstr+=j
    print(fmt.format(content_list[x], content_list[x + 1] ,pinstr,numstr))

Sample Output

Record file (record.txt)

Add a comment
Know the answer?
Add Answer to:
PYTHON: I have a numpy array that holds a name and a phone number such as...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python with Pandas dataframe I have a csv file that contains a large number of columns...

    Python with Pandas dataframe I have a csv file that contains a large number of columns and rows. I need to write a script that concatenates some elements of the first row with some elements of the 2 row. Something like # if data[1][0] starts with ch then concatenate the element right below it. I have attached a picture of just a sample of my data. The booleans have to stay on there as is. But I must drop the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT