Reputation: 3667
I have a pandas series y which I generated by group by and looks like
Item Attribute Feature
aaaa x1 f1,f2
x2 f3,f4
bbbb x3 f5
x4 f6
I want to generate a tab delimted output that looks exactly as above. When I try
y.to_csv('out',sep = "\t")
I get
Item Attribute Feature
aaaa x1 f1,f2
aaaa x2 f3,f4
bbbb x3 f5
bbbb x4 f6
I want to avoid repetition of elements in the "Item" column. Can you help
Upvotes: 2
Views: 1682
Reputation: 294488
The text you want isn't csv
.
Assume df
is your dataframe or series:
with open('out.txt', 'w') as f:
f.write(df.__repr__())
to_txt
functionimport 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)
to_text(df, 'test_file.txt')
Upvotes: 1