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)
Python dictionaries are essentially unordered.
You can explicitly order the columns like this:
df = df[['A','B','C']]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With