8/10 Cleaning and Preparing data in Python

Screen Link:

My Code:

test_data = ["1912", "1929", "1913-1923",
             "(1951)", "1994", "1934",
             "c. 1915", "1995", "c. 1912",
             "(1988)", "2002", "1957-1959",
             "c. 1955.", "c. 1970's", 
             "C. 1990-1999"]

bad_chars = ["(",")","c","C",".","s","'", " "]

def strip_characters(string):
    for char in bad_chars:
        string = string.replace(char,"")
    return string

stripped_test_data = ['1912', '1929', '1913-1923',
                      '1951', '1994', '1934',
                      '1915', '1995', '1912',
                      '1988', '2002', '1957-1959',
                      '1955', '1970', '1990-1999']
def process_date(date):
    if '-' in date:
        split_date = date.split('-')
        date_one = split_date[0]
        date_two = split_date[1]
        date = (int(date_one)+ int(date_two)) / 2
        date = round(date)
    else:
        date = int(date)
        return date
processed_test_data = []
for d in stripped_test_data:
    date = process_date(d)
    processed_test_data.append(date)
    
for row in moma:
    date = row[6]
    date = strip_characters(date)
    date = process_date(date)
    row[6] = date
        
            

When I run the code I get the error message below

    Value of moma is not what we expected.
    Value of processed_test_data is not what we expected.

2 Likes

@sharonomojah This might happen when you don’t assign a processed data to “moma”.

You can take a look at this Question. Other user had the same problem.

I hope it helps :grinning:

1 Like

Hello @vinigomesaraujo , I have tried to check how to solve it but it’s still not working

Hello @sharonomojah, did you took a lot in the question that I marked before?

def process_date(data):

    if '-' in data:
        before , after = data.split('-')
        data = round((int(before) + int(after)) / 2)
    else:
        data = int(data)
        
    return data

processed_test_data = []
for row in stripped_test_data:
    Try_test = row
    Try_test = process_date(Try_test)
    processed_test_data.append(Try_test)
    row = Try_test
    
for row in moma:
    Date = row[6]
    Date = strip_characters(Date)
    Date = process_date(Date)
    row[6] = Date

This seems to be working well :grinning:

Hi @sharonomojah , I think the codes given by @vinigomesaraujo should work perfectly and if not working still ;please check on the;

  • processed_test_data, isn’t defined in your code lines as expected in Dataquest’s platform, so you can have it as an empty list.