Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

writing csv in python pandas, need to change order of columns and add blank columns

I have imported a csv file into python and I'm using pandas. I need to output a new csv file containing only some of the data, and in a different order with blank columns. The new csv file will be used to import data from one system into, and the data need to line up.

so if the original csv file had the following columns

"date" "department" "name" "title" "employee id"

I need the rows of the csv file to read

"name",,,,,"department",,,,"date",,

I have deleted the columns that I don't need:

del df["title"],def["employee id"]

I wrote a bunch of blank columns:

df[a] = '';
df[b] = '';
df[c] = '';

When I write them to csv in the order I want

df.to_csv('outfile.csv', cols=["name","a","b","c","department","d","e","f","date","g","h"], index=False,header=False)

It comes out

date,department,,,,,,,,,,,name,,

Should I be working with the csv module for this particular type of project? I'm scouring the documentation, but having trouble figuring how what I'm reading applies to my task

like image 835
mattrweaver Avatar asked Jan 07 '23 05:01

mattrweaver


1 Answers

It'll be easier in my opinion to reindex your df, this will put the cols in the order you desire and where columns don't exist put NaN values there:

df.reindex(columns=["name","a","b","c","department","d","e","f","date","g","h"]‌​).to_csv('outfile.csv', index=False,header=False)
like image 145
EdChum Avatar answered Jan 26 '23 19:01

EdChum