1

I created one program in python in which I used collaborative filtering to find Item based CF. My code is,

# Create a placeholder items for closes neighbours to an item
data_neighbours = pd.DataFrame(index=data_ibs.columns,columns=[range(1,13)])
print data_neighbours
# Loop through our similarity dataframe and fill in neighbouring item names
for i in range(0,len(data_ibs.columns)):
    ##use sort_values(...)
    data_neighbours.ix[i,:10] = data_ibs.ix[0:,i].sort_values(ascending=[1, 0])[:10].index
    data_neighbours.ix[i,:10] = data_ibs.ix[0:,i].sort_values(ascending=[1, 0])[:10].index



print data_neighbours.ix[i,:10]

Output is in table:


         user         1          2          3
    0   192.   c1s1b1p3   c1s1b1p4   c1s1b1p5
    1   192.   c1s1b1p3   c1s1b1p4   c1s1b1p5
    2   192.   c1s1b1p3   c1s1b1p4   c1s1b1p5
    3   192.   c1s1b1p3   c1s1b1p4   c1s1b1p5
    4   192.   c1s1b1p7   c1s1b1p8  c1s1b1p10
    5   192    c1s1b1p5   c1s1b1p6   c1s1b1p7
    6   192.   c1s1b1p3   c1s1b1p4   c1s1b1p5
    7   192.   c1s1b1p8   c1s1b1p9   c1s1b1p4
    8   192.   c1s1b1p6  c1s1b1p10   c1s1b1p5
    9   192.   c1s1b1p3   c1s1b1p5   c1s1b1p7
    10  192.   c1s1b1p6   c1s1b1p8   c1s1b1p9

This is table display on cmd prompt I want to save this table in csv file. How I can create csv file to store this table.

4
  • 2
    Look at pandas.DataFrame.to_csv. And always try googling first, this looks like a very common and basic question. Commented Aug 26, 2016 at 12:25
  • I did google, but didn't get it.and I am new in python. hence why I'm facing such problems. Commented Aug 26, 2016 at 12:31
  • I ran above code I got this error Commented Aug 26, 2016 at 12:34
  • sw.writerow(rows) _csv.Error: sequence expected Commented Aug 26, 2016 at 12:34

2 Answers 2

1
with open('hi.csv','wb') as ff:
    sw=csv.writer(ff,delimiter=',',quoting=csv.QUOTE_MINIMAL) 
       for rows in data_neighbours: 
           sw.writerow(rows)

data_neighbours must contain lists in row wise....

Sign up to request clarification or add additional context in comments.

5 Comments

the ff.close() is not needed. the with block takes care of all that.
sw.writerow(rows) _csv.Error: sequence expected
yeah, how your data_neighbours contains ? as I mentioned it should be like this [ [row1],[row2],[row3]....]
I want to print- " data_neighbours.ix[i,:10] " this is output of data_neighbours.ix[i,:10]
I don't want to print data_neighbours
0

You should consider to use the csv module. In the documentation there is all the explanations and examples you need to create your csv file.

Comments

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.