Using Pandas dataframes to write to complex format layout
<p>I am trying to create a file which has a very specific format, which means it is very difficult for me to operate and save with pandas alone.</p>

<p>Consider this:</p>

<pre><code>FILE = open('writeFileTest' + ".trc", "w")

# Print header information
FILE.write('A\tB\tC\n')
FILE.write('\t\tD\tE\tF\tG\n')
</code></pre>

<p>This will produce some headers which look roughly as so:</p>

<pre><code>A  B  C
      D  E  F  G
</code></pre>

<p>Now lets say that I also have a pandas dataframe that looks like this e.g:</p>

<pre><code>&gt;&gt;&gt;import pandas as pd
&gt;&gt;&gt;import numpy as np
&gt;&gt;&gt;pd.DataFrame(np.random.randn(5, 6))
          0         1         2         3         4         5
0  0.215413  0.075976  0.516593  1.699469  1.382774 -0.604032
1  0.156343  0.918240  0.728018 -0.975881 -1.034713 -1.920139
2  1.486848 -0.762764 -0.232464  1.824197 -0.321638  0.187009
3 -1.125282 -0.419082  1.025092  1.381589  0.369712  0.043958
4 -0.118296  0.699864  0.796202 -0.560172 -1.046126  0.398537
</code></pre>

<p>How do I combine both to produce this:</p>

<pre><code>A         B         C
                    D         E         F         G
0.215413  0.075976  0.516593  1.699469  1.382774 -0.604032
0.156343  0.918240  0.728018 -0.975881 -1.034713 -1.920139
1.486848 -0.762764 -0.232464  1.824197 -0.321638  0.187009
-1.125282 -0.419082  1.025092  1.381589  0.369712  0.043958
0.118296  0.699864  0.796202 -0.560172 -1.046126  0.398537
</code></pre>

<p>My actual file is obviously a lot more complicated than this, which is why I cannot use pandas, and my data is much much larger. Hence, how can one combine both approaches? I should also mention that every is separated by <code>\t</code> tabs. </p>

<p>EDIT:</p>

<p>There is also the possibility of doing all of this in pure pandas (I think, but I obviously do not know how, hence this question). </p>

<p>Then instead we would treat this whole thing:</p>

<pre><code>A         B         C
                    D         E         F         G
0.215413  0.075976  0.516593  1.699469  1.382774 -0.604032
0.156343  0.918240  0.728018 -0.975881 -1.034713 -1.920139
1.486848 -0.762764 -0.232464  1.824197 -0.321638  0.187009
-1.125282 -0.419082  1.025092  1.381589  0.369712  0.043958
0.118296  0.699864  0.796202 -0.560172 -1.046126  0.398537
</code></pre>

<p>as a pandas dataframe <code>foo</code> upon which we would do <code>foo.to_csv(foo.csv,sep='\t')</code> and then change the file extension afterwards. But then one would instead have to deal with empty dataframe cells and ensure that pandas treats them as empty upon save.</p>

<p>You could open the file, write the header manually and then dump the data frame.  Try this:</p>

<pre><code>import pandas as pd
import numpy as np

data = np.random.randint(0,10, (4,6))

df = pd.DataFrame(data, columns=list('abcdef'))

header1 = 'A\tB\tC\t\t\t\n'
header2 = '\t\tD\tE\tF\tG\n'
with open('./out.tsv','w') as fp:
    fp.write(header1)
    fp.write(header2)
    df.to_csv(fp, sep='\t', header=False, index=False)
</code></pre>

python pandas