list1 = [1,2,3]
list2 = [3,5,6]
list3 = [4,5,6]
I want output in csv file as:
A B C
1 3 4
2 5 5
3 6 6
I tried using pandas as well as np.savetxt()
but it doesnt work!
Use DataFrame
contructor with to_csv
:
pd.DataFrame({'A': list1, 'B': list2, 'C':list3}).to_csv('file.csv', index=False)
If want custom order of columns:
(pd.DataFrame({'A': list1, 'B': list2, 'C':list3}, columns=['B','A','C'])
.to_csv('file.csv', index=False))
An alternative if you do not want to use external libraries:
import csv
with open('foo.csv', "wb") as csv_file:
writer = csv.writer(csv_file, delimiter=',')
writer.writerow(['A', 'B', 'C'])
for i in range(0, len(list1)):
writer.writerow([list1[i],list2[i],list3[i]])
尝试这个:
np.savetxt('test.csv', np.c_[l1,l2,l3], delimiter=' ', header='A B C',fmt='%d',comments='')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.