4

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

mattrweaver
  • 589
  • 2
  • 11
  • 29
  • I think it'll be easier to reindex after deleting the cols: `df.reindex(columns=["name","a","b","c","department","d","e","f","date","g","h"]).to_csv('outfile.csv', index=False,header=False)` – EdChum Nov 06 '15 at 13:32
  • Thanks, that did it! – mattrweaver Nov 06 '15 at 13:44

1 Answers1

7

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)
EdChum
  • 294,303
  • 173
  • 671
  • 486