Converting pandas dataframe to csv
<p><a href="https://i.stack.imgur.com/ZlUc1.png" rel="nofollow"><img src="https://i.stack.imgur.com/ZlUc1.png" alt="enter image description here"></a>
I have the dataframe above and I wish to convert it into a csv file.<br>
I am currently using <code>df.to_csv('my_file.csv')</code> to convert it but I want to leave 3 blanks columns. For the rows of the file above I have the following procedure.  </p>

<pre><code>dirname = os.path.dirname(os.path.abspath(__file__))
csvfilename = os.path.join(dirname, 'MainFile.csv')
with open(csvfilename, 'wb') as output_file:
    writer = csv.writer(output_file, delimiter=',')
    writer.writerow([])
    writer.writerow(["","Amazon","Weekday","Weekend"])
    writer.writerow(["","Ebay",wdvad,wevad])
    writer.writerow(["","Kindle",wdmpv,wempv])
    writer.writerow([])
</code></pre>

<p>I want to incorporate the data frame right after the blanks space with three blank columns. How can I add the dataframe in continuation to the existing csv file so that I can also add more rows with data after the dataframe. </p>

<p>Consider outputting data frame initially as is to a temp file. Then, during creation of the <em>MainCSV</em>, read in temp file, iteratively writing lines, then destroy temp file. Also, prior to writing dataframe to csv, create the three blank columns. </p>

<p>Below assumes you want two tasks: 1) three blank columns and 2) writing dataframe values below Amazon/Ebay/Kindle row headers. Example data uses random normal values and scalar values for <em>wdvad</em>, <em>wevad</em>, <em>wdmpv</em>, <em>wempv</em> are the string literals of their names:</p>

<pre><code>import csv, os
import pandas as pd

# TEMP DF CSV
dirname = os.path.dirname(os.path.abspath(__file__))    
df = pd.DataFrame([np.random.normal(loc=3.0, scale=1.0, size=24)*1000 for i in range(7)],
                  index=['Monday', 'Tuesday', 'Wednesday', 'Thursday', 
                         'Friday', 'Saturday', 'Sunday'])
df['Blank1'], df['Blank2'], df['Blank3'] = None, None, None   

df.to_csv(os.path.join(dirname, 'temp.csv'))       # OUTPUT TEMP DF CSV

# MAIN CSV
csvfilename = os.path.join(dirname, 'MainFile.csv')
tempfile = os.path.join(dirname, 'temp.csv')

wdvad = 'wdvad'; wevad = 'wevad'; wdmpv = 'wdmpv'; wempv = 'wempv'

with open(csvfilename, 'w', newline='') as output_file:
    writer = csv.writer(output_file)
    writer.writerow([""])
    writer.writerow(["","Amazon","Weekday","Weekend"])
    writer.writerow(["","Ebay",wdvad,wevad])
    writer.writerow(["","Kindle",wdmpv,wempv])
    writer.writerow([""])

    with open(tempfile, 'r') as data_file:
        for line in data_file:
            line = line.replace('\n', '')
            row = line.split(",")            
            writer.writerow(row)

os.remove(tempfile)                               # DESTROY TEMP DF CSV
</code></pre>

<p><strong>Output</strong></p>

<p><a href="https://i.stack.imgur.com/qVOCL.png" rel="nofollow"><img src="https://i.stack.imgur.com/qVOCL.png" alt="CSV File Output"></a></p>

python csv pandas