USING PYTHON 1 Work with Census Data Dowload the following f

USING PYTHON

1. Work with Census Data: Dowload the following file:

http://www.census.gov/population/www/censusdata/files/urpop0090.txt

The census _le is a text _le with data for the 10-year census from 1900 to 1990 (e.g.,

1900, 1910, 1920, . . . ). It has population data for each state as well as regional

and overall data. Each state is on its own line, but the data are grouped so that

only three decades of data are on each line complicating the task of extracting

the data. In addition, the data are further broken down into total, urban, rural,

and percentages. Write a program that for any census year input (e.g., 1970)

the program will print the state and its total population with the minimum and

maximum. For example:

Enter census year 1900 to 1990: 1970

Minimum: (302853, \'Alaska\')

Maximum: (19971069, \'California\')

The output is displayed as a tuple as a hint to assist with solving the problem

rather than illustrating readable output. Some points to consider:

(a) Begin by generating clean data: there are footnotes that need to be elim-

inated, numbers contain commas, some rows (lines) have data you are not

interested in (e.g., region data), you are not interested in all columns (e.g.,

percentages), and so on. Simply printing the lines with extraneous data re-

moved is a good start.

(b) You will likely want to combine multiple state name strings into one, e.g.,

\\New\" \\York\" becomes \\New York.\"

(c) A tuple (population, state) provides a way to tag population data with a state

in a way that allows a list of tuples to be sorted (remember that by default,

sorting uses the first value).

Solution

list_of_states = [
        \"Maine\", \"New Hampshire\", \"Vermont\", \"Massachusetts\", \"Rhode Island\", \"Connecticut\", \"New York\", \"New Jersey\",
        \"Pennsylvania\", \"Ohio\",
        \"Indiana\", \"Illinois\", \"Michigan\", \"Wisconsin\", \"Minnesota\", \"Iowa\", \"Missouri\", \"North Dakota\", \"South Dakota\",
        \"Nebraska\",
        \"Kansas\", \"Delaware\", \"Maryland\", \"District of Columbia\", \"Virginia\", \"West Virginia\", \"North Carolina\",
        \"South Carolina\", \"Georgia\", \"Florida\",
        \"Kentucky\", \"Tennessee\", \"Alabama\", \"Mississippi\", \"Arkansas\", \"Louisiana\", \"Oklahoma\", \"Texas\", \"Montana\",
        \"Idaho\", \"Wyoming\", \"Colorado\", \"New Mexico\", \"Arizona\", \"Utah\", \"Nevada\", \"Washington\", \"Oregon\", \"California\",
        \"Alaska\", \"Hawaii\"
        ]

def group_census_data_based_on_the_year(states_and_population_data):
    cleaned_up_census_data = [
        [], [], [], [], [], [], [], [], [], [],
    ]
    for item in states_and_population_data:
        census_values = states_and_population_data[item]
        index = 0
        for x in reversed(census_values):
            tup = (int(x.replace(\",\", \"\")), item)
            cleaned_up_census_data[index].append(tup)
            index += 1

    # write output to file
    for line in cleaned_up_census_data:
        with open(\'file_to_write.txt\', \'a\') as f:
            f.write(str(line))
            f.write(\"\ \")

    return cleaned_up_census_data


def get_max_and_min_census_data_for_year(year, sanitized_census_data):
    mapping_of_year_and_index = {
        1990: 9,
        1980: 8,
        1970: 7,
        1960: 6,
        1950: 6,
        1940: 4,
        1930: 3,
        1920: 2,
        1910: 1,
        1900: 0
    }

    if year not in mapping_of_year_and_index:
        print(\"The year you entered is not valid\")
        return

    # Find max and min value from the sanitized census data set
    year_index = mapping_of_year_and_index[year]
    data_for_requested_year = sanitized_census_data[year_index]

    min_population = data_for_requested_year[0][0]
    max_population = data_for_requested_year[0][0]
    max_tuple = ()
    min_tuple = ()
    for x in data_for_requested_year:
        if x[0] > max_population:
            max_tuple = x
            max_population = x[0]
        elif x[0] < min_population:
            min_tuple = x
            min_population = x[0]

    print(\"Minimum: \" + str(min_tuple))
    print(\"Maximum: \" + str(max_tuple))


def clean_up_data_set(year):
    lines = []
    states_and_population_data = {}

    data_file = open(\"urpop0090.txt\", \"r\")
    for line in data_file:

        # remove new line
        cleaned_line = line.rstrip()

        # ignore empty lines
        if cleaned_line:

            for state in list_of_states:
                without_pre_spaces = line.lstrip()
                if without_pre_spaces.startswith(state):
                    splitter = without_pre_spaces[len(state):].split()

                    # some of the entries contain \'r *2 and *1\' inside them, we want to go ahead and remove it
                    weird_characters = [\"r\", \"*1\", \"*2\"]

                    cleaned_list = []
                    for item in splitter:
                        if item not in weird_characters:
                            cleaned_list.append(item)

                    # The algorithm that actually builds a data set that we can actually use
                    # for meaningful data processing. It essentially builds up a hashmap for every state
                    # with all its population value
                    length_of_row = (len(cleaned_list) / 5)
                    start_index = 0
                    for index in range(0, int(length_of_row)):
                        total_population = str(cleaned_list[start_index])
                        start_index += 5

                        if state in states_and_population_data:
                            states_and_population_data[state].append(total_population)
                        else:
                            states_and_population_data[state] = [total_population]

                    lines.append(cleaned_list)
                    break

    sanitized_census_data = group_census_data_based_on_the_year(states_and_population_data)

    get_max_and_min_census_data_for_year(year, sanitized_census_data)


def main():
    user_input = input(\"Enter census year 1900 to 1990: \")

    if not user_input:
        print(\"Sorry but you must enter a valid input\")
        return

    try:
        clean_up_data_set(int(user_input))
    except ValueError:
        print(\"Please enter all numbers\")


if __name__ == \'__main__\':
    main()
  

  

USING PYTHON 1. Work with Census Data: Dowload the following file: http://www.census.gov/population/www/censusdata/files/urpop0090.txt The census _le is a text
USING PYTHON 1. Work with Census Data: Dowload the following file: http://www.census.gov/population/www/censusdata/files/urpop0090.txt The census _le is a text
USING PYTHON 1. Work with Census Data: Dowload the following file: http://www.census.gov/population/www/censusdata/files/urpop0090.txt The census _le is a text
USING PYTHON 1. Work with Census Data: Dowload the following file: http://www.census.gov/population/www/censusdata/files/urpop0090.txt The census _le is a text

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site