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
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)
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