Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas Writing Dataframe Columns to csv

Tags:

python

pandas

csv

I'm writing a script to reduce a large .xlsx file with headers into a csv, and then write a new csv file with only the required columns based on header name.

import pandas import csv  df = pandas.read_csv('C:\\Python27\\Work\\spoofing.csv')  time = df["InviteTime (Oracle)"] orignum = df["Orig Number"] origip = df["Orig IP Address"] destnum = df["Dest Number"]  df.to_csv('output.csv', header=[time,orignum,origip,destnum]) 

The error I'm getting is with that last bit of code, and it says

ValueError: Writing 102 cols but got 4 aliases 

I'm sure i'm overlooking something stupid, but I've read over the to_csv documentation on the pandas website and I'm still at a loss. I know I'm using the to_csv parameters incorrectly but I can't seem to get my head around the documentation I guess.

Any help is appreciated, thanks!

like image 932
Harrison Boles Avatar asked Feb 25 '14 16:02

Harrison Boles


People also ask

How do I write Pandas DataFrame to CSV?

By using pandas. DataFrame. to_csv() method you can write/save/export a pandas DataFrame to CSV File. By default to_csv() method export DataFrame to a CSV file with comma delimiter and row index as the first column.

Can Pandas write to CSV?

Pandas is a very powerful and popular framework for data analysis and manipulation. One of the most striking features of Pandas is its ability to read and write various types of files including CSV and Excel.

What does to_csv return?

to_csv() function write the given series object to a comma-separated values (csv) file/format. Parameter : path_or_buf : File path or object, if None is provided the result is returned as a string.


1 Answers

The way to select specific columns is this -

header = ["InviteTime (Oracle)", "Orig Number", "Orig IP Address", "Dest Number"] df.to_csv('output.csv', columns = header) 
like image 160
user1827356 Avatar answered Sep 19 '22 23:09

user1827356