0

i have a list. which has 8 lists inside it and each list have 50 elements

ex - [[list 1 with 50 elements ],[list 2 with 50]........,[list 8 with 50]]

i want to write the list to a CSV file as all 8 lists inside the outer list as 8 columns and 50 items in each.But when i try to write it it brings an error message saying i need 50 columns instead of 8. how to fix this problem?

df = ps.DataFrame( data = newresult , columns=['1','2','3','4','5','6','7','8'])
df.to_csv("k-mean-test.csv", index = False)
1
  • What did you use csv module? or DictWriter? Commented Nov 8, 2014 at 14:53

1 Answer 1

1
import pandas as pd
lists = [range(10)] * 3
df = pd.DataFrame(lists)
print df.to_csv(index=False, header=False)

This gives you:

0,1,2,3,4,5,6,7,8,9
0,1,2,3,4,5,6,7,8,9
0,1,2,3,4,5,6,7,8,9
Sign up to request clarification or add additional context in comments.

4 Comments

mmmm yeah but i am taking data from a long process john. there is no way i could simplified result as that.
If your data is as you said it is, this solution should work. Your list of lists is longer and wider than mine, sure, but the rest looks the same.
i am sorry. I understood it wrong. Yeah it come out this way. But i want transpose of your result. First column 0-9, 2nd column 0-9 and third column 0-9
@Optimuskck: OK then do df.T.to_csv(...).

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.