I have a very large pandas df I am writeing out to csv. I need to add a second header row containing the data types. The below code works but produces a third unexpected empty row in the CSV:
#! /usr/bin/env python
import pandas as pd
df = pd.DataFrame([[1, 2], [3, 4]], columns=list('AB'))
# get count of header columns, add REAL for each one
types_header_for_insert = list(df.columns.values)
for idx, val in enumerate(types_header_for_insert):
types_header_for_insert[idx] = 'REAL'
# count number of index columns, then add STRING for each one
index_count = len(df.index.names)
for idx in range(0, index_count):
df.reset_index(level=0, inplace=True)
types_header_for_insert.insert(0, 'STRING')
# insert the new types column
df.columns = pd.MultiIndex.from_tuples(zip(df.columns, types_header_for_insert))
print df.columns.values
df.to_csv("./test.csv", index=False)
output:
index,A,B
STRING,REAL,REAL
,,
0,1,2
1,3,4
How can I get rid of this extra blank row? Where does it come from?