Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: Random order of columns when writing DataFrame to csv

Tags:

python

pandas

I'm working on a crawling function that writes continuously to output.csv. If it's the first pass, it will write the first row with the column header to a blank file. For the following passes, it will append with no header.

My issue is that the column order gets jumbled. I would like them to follow the order that I have specified in df = pd.DataFrame.

import pandas as pd

input = pd.read_csv(input.csv, delimiter=",")

run = 0

def crawl(x):

    global run
    run = run + 1

    #Assign strings a, b, c

    df = pd.DataFrame({"A": [a], "B": [b], "C": [c]})

    if run == 1:
        df.to_csv("output.csv")
    if run != 1:
        df.to_csv("output.csv", header=None, mode="a")

input["X"].apply(crawl, axis=1)
like image 596
P A N Avatar asked Feb 09 '23 05:02

P A N


1 Answers

Python dictionaries are essentially unordered.

You can explicitly order the columns like this:

df = df[['A','B','C']]
like image 84
chrisb Avatar answered Feb 11 '23 19:02

chrisb