Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
171 views
in Technique[技术] by (71.8m points)

Python pandas: how to write to csv in certain cells without header

I have the following variables.

num1 = '1'
num2 = '2'
num3 = '3'
num4 = '4'
num5 = '5'
num6 = '6'
num7 = '7'
num8 = '8'

Using jupyter notebook, I want to write to csv with 4 values on the 1st row to the left and 4 values on the 2nd row to the left without header and index.

I am expecting to see this in csv. How should I do it? enter image description here

In the documentation, there is this example:

df = pd.DataFrame({'name': ['Raphael', 'Donatello'],
                   'mask': ['red', 'purple'],
                   'weapon': ['sai', 'bo staff']})
df.to_csv(index=False)

I am not sure how to modify this part

{'name': ['Raphael', 'Donatello'],
                   'mask': ['red', 'purple'],
                   'weapon': ['sai', 'bo staff']}

to fit mine.

question from:https://stackoverflow.com/questions/65858447/python-pandas-how-to-write-to-csv-in-certain-cells-without-header

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
 df.to_csv('test.csv', header=False, index=False)

i fix it after you edited the post.

list1 = [num1,num2,num3,num4]
list2 = [num5,num6,num7,num8]
list1, list2
df = pd.DataFrame(list1).T
df = df.append(pd.Series(list2),ignore_index=True)
df

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...