additional column when saving pandas data frame to csv file
<p>Here the the code to process and save csv file, and raw input csv file and output csv file, using pandas on Python 2.7 and wondering why there is an additional column at the beginning when saving the file? Thanks.</p>

<pre><code>c_a,c_b,c_c,c_d
hello,python,pandas,0.0
hi,java,pandas,1.0
ho,c++,numpy,0.0

sample = pd.read_csv('123.csv', header=None, skiprows=1,
    dtype={0:str, 1:str, 2:str, 3:float})
sample.columns = pd.Index(data=['c_a', 'c_b', 'c_c', 'c_d'])
sample['c_d'] = sample['c_d'].astype('int64')
sample.to_csv('saved.csv')
</code></pre>

<p>Here is the saved file, there is an additional column at the beginning, whose values are <code>0, 1, 2</code>.</p>

<pre><code>cat saved.csv
,c_a,c_b,c_c,c_d
0,hello,python,pandas,0
1,hi,java,pandas,1
2,ho,c++,numpy,0
</code></pre>

<p>The additional column corresponds to the index of the dataframe and is aggregated once you read the CSV file. You can use this index to slice, select or sort your DF in an effective manner.</p>

<p><a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Index.html" rel="nofollow">http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Index.html</a></p>

<p><a href="http://pandas.pydata.org/pandas-docs/stable/indexing.html" rel="nofollow">http://pandas.pydata.org/pandas-docs/stable/indexing.html</a></p>

<p>If you want to avoid this index, you can set the <code>index</code> flag to <code>False</code> when you save your dataframe with the function <code>pd.to_csv</code>. Also, you are removing the header and aggregating it later, but you can use the header of the CSV to avoid this step.</p>

<pre><code>sample = pd.read_csv('123.csv', dtype={0:str, 1:str, 2:str, 3:float})
sample.to_csv('output.csv', index= False)
</code></pre>

<p>Hope it helps :)</p>

python python-2.7 csv pandas dataframe