[英]Pandas: Random order of columns when writing DataFrame to csv
I'm working on a crawling function that writes continuously to output.csv
. 我正在处理一个连续写入
output.csv
的爬行函数。 If it's the first pass, it will write the first row with the column header
to a blank file. 如果是第一次传递,它会将带有列
header
的第一行写入空白文件。 For the following passes, it will append
with no header. 对于以下传递,它将
append
没有标题。
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
. 我希望他们遵循我在
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.