To have the index as an implicit column in the csv:
import pandas as pd
import io
df = pd.DataFrame(dict(data=[5, 7, 3]))
with io.open('df.csv', 'wb') as file: df.to_csv(file, header=False)
gives
0,5
1,7
2,3
or if you have more interesting index values then use
import pandas as pd
import io
df = pd.DataFrame(dict(data=[5, 7, 3]))
df.reset_index(inplace=True)
with io.open('df.csv', 'wb') as file: df.to_csv(file)
which gives
,index,data
0,0,5
1,1,7
2,2,3
to get spaces the use
import pandas as pd
import io
df = pd.DataFrame(dict(data=[5, 7, 3]))
df.index.rename
with io.open('df.csv', 'wb') as file: df.to_csv(file, sep=" ", header=False)
which gives
0 5
1 7
2 3
although spaces are probably best to avoid.
The closer resemblance to your |header is perhaps
import pandas as pd
import io
df = pd.DataFrame(dict(data=[5, 7, 3]))
df.index.rename
df.reset_index(inplace=True)
for col in df.columns:
df[col] = df[col].apply(lambda x: '|' + col + ' ' + str(x))
with io.open('df.csv', 'wb') as file: df.to_csv(file, sep=" ", header=False, index=False, quotechar=' ')
giving
|index 0 |data 5
|index 1 |data 7
|index 2 |data 3
sep=' '