[Python] looping over every line and appending to Pandas dataframe

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

  1. Stack

    Stack Membro Participativo

    Rather than use pd.read_csv on a file, I've found that I must first loop through the file, line by line, and strip the newline character:

    with open('input.txt', 'r') as data:
    for line in data:
    print(line.strip())


    I'd end up with something like this:

    44 ----- 2025 1498
    45 ----- 2025 5598
    46 ----- 2025 1478
    47 ----- 2025 1411


    What is the best way to append each line to a new dataframe? Is seperating by whitespace the ideal method?

    Continue reading...

Compartilhe esta Página