0

I have a list named items items=['a' , 'b','c'] Code is:

df = pandas.DataFrame(items)
df.to_csv("myfile.csv",headers=None,index=False)

the values written to the file are in different rows but same column.(vertically written) But I want the values to be written as : a b c ie. in same row but different column. Help please

1 Answer 1

2

You get each element in different rows because you load the df as that way.

If you want in different column I would suggest to do transpose,

df = df.T

or you can load as one row like below,

items=[['a' , 'b','c']]
df = pd.DataFrame(items)
df
Out[22]: 
   0  1  2
0  a  b  c

And then write the output to csv,

eg:

df = pandas.DataFrame(items)
df = df.T
df.to_csv("myfile.csv",headers=None,index=False)

df = pd.DataFrame(items)

df
Out[5]: 
   0
0  a
1  b
2  c

df.T
Out[11]: 
   0  1  2
0  a  b  c
Sign up to request clarification or add additional context in comments.

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.