1

I have a Pandas dataframe with the following data

0 5
1 7
2 3

The first column is the index.

What is the easiest way to get this written to a csv file (space delimited) so the output will look like this?

|index 0 |features 5
|index 1 |features 7
|index 2 |features 3

By csv file, I mean writing a file like this:

test.to_csv('test_map2.txt', sep=' ', header=None, index=False)
3
  • That is not a csv. Please show at actual csv as you would like it, using commas to separate values. Commented Jan 10, 2017 at 9:48
  • I have added some information to my question. By csv file, I mean the output of the Pandas to_csv function. Not necessarily that the separator is a comma. In my case I want to use space as the separator. Commented Jan 10, 2017 at 9:52
  • first convert your dataframe to the desired format, you can use apply function on the index and column then save it with sep=' ' Commented Jan 10, 2017 at 9:55

2 Answers 2

1

you can proceed as follows

test.index = test.index.map(lambda x:"|index " + str(x))
test.ix[:,0] = test.ix[:,0].apply(lambda x:'|features ' + str(x))
test.to_csv('test_map2.txt', sep=' ', header=None, index=False)
Sign up to request clarification or add additional context in comments.

3 Comments

This approach looks like exactly what I want. The first statement worked fine, but on the second one failed with an error (TypeError: unhashable type: 'slice'). I had to change "test[:,0]" into "test[test.columns[0]]".
that is a good approach, you can also use the ix, indexing, i modified it now
Great! Notice that str(x) would also be required at the end of line #2
0

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 

1 Comment

But, how do I get the text "|index " prefixed to the values in the index column and the text "|features " prefixed to the second column in the output file (and in the dataframe if that is required)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.