writing pandas series into tab separated file
<p>I have a pandas series y which I generated by group by and looks like </p>

<pre><code>Item   Attribute  Feature
aaaa   x1         f1,f2
       x2         f3,f4
bbbb   x3         f5
       x4         f6
</code></pre>

<p>I want to generate a tab delimted output that looks exactly as above. When I try</p>

<pre><code>y.to_csv('out',sep = "\t")
</code></pre>

<p>I get </p>

<pre><code>Item   Attribute   Feature
aaaa   x1         f1,f2
aaaa   x2         f3,f4
bbbb   x3         f5
bbbb   x4         f6
</code></pre>

<p>I want to avoid repetition of elements in the "Item" column. Can you help</p>

<p>The text you want isn't <code>csv</code>.</p>

<p>Assume <code>df</code> is your dataframe or series:</p>

<pre><code>with open('out.txt', 'w') as f:
    f.write(df.__repr__())
</code></pre>

<hr>

<h3><code>to_txt</code> function</h3>

<pre><code>import pandas as pd

def to_txt(df, fn=None):
    x, c, r = ['expand_frame_repr', 'max_columns', 'max_rows']
    current_max_col = pd.get_option(c)
    current_max_row = pd.get_option(r)
    current_xpd_frm = pd.get_option(x)

    pd.set_option(x, False)
    pd.set_option(c, df.shape[1])
    pd.set_option(r, df.shape[0])

    if fn is not None:
        with open(fn, 'w') as f:
            f.write(df.__repr__())
    else:
        return df.__repr__()

    pd.set_option(x, current_xpd_frm)
    pd.set_option(c, current_max_col)
    pd.set_option(r, current_max_row)
</code></pre>

<h3>Demonstration</h3>

<pre><code>to_text(df, 'test_file.txt')
</code></pre>

python pandas