[Python] Reading Census BPS Historical data into python

Discussão em 'Python' iniciado por Stack, Fevereiro 5, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to read the data in the files here into python. Currently I'm using pandas to do this

    import pandas as pd
    url = "https://www.census.gov/construction/bps/txt/tb3u201910.txt"
    df = pd.read_fwf(url,skiprows=8,skipfooter=6,header=1)


    This basically works except for lines that have a break in them. For instance, line 29 continues on to line 30. In the data frame that data turns into

    122 12060 Atlanta-Sandy Springs-Alpharetta, NaN NaN NaN NaN NaN NaN NaN


    with a second line that has

    GA NaN 3728 2242.0 4.0 0.0 1482.0 20.0 100.0


    The output I'm hoping to achieve is

    122 12060 Atlanta-Sandy Springs-Alpharetta,GA 3728 2242.0 4.0 0.0 1482.0 20.0 100.0


    Is there anything I can do to help pandas recognize the line continuation? Alternatively, is there a way to modify my current dataframe to fix these errors? I'm open to both pandas and non-pandas solutions

    Continue reading...

Compartilhe esta Página