import pandas as pd
list1 = [1,2,3,4,5]
df = pd.DataFrame(list1)
df.to_csv('filename.csv', index=False)
#index =false removes unnecessary indexing/numbering in the csv
# Convert a list into rows for a column in csv
import csv
for_example = [1, 2, 3, 4, 5, 6]
with open('output.csv', 'w', newline='') as csv_1:
csv_out = csv.writer(csv_1)
csv_out.writerows([for_example[index]] for index in range(0, len(for_example)))
import pandas as pd
df = pd.read_csv('csvfile.csv', sep=',')
list_of_lists = [list(x) for x in df.values]
print(list_of_lists)
import pandas as pd
list1 = [1,2,3,4,5]
df = pd.DataFrame(list1)
df.to_csv('test.csv') ##It will include index also
df.to_csv('test.csv', index=False) #Without Index
import csv
# field names
fields = ['Name', 'Branch', 'Year', 'CGPA']
# data rows of csv file
rows = [ ['Nikhil', 'COE', '2', '9.0'],
['Sanchit', 'COE', '2', '9.1'],
['Aditya', 'IT', '2', '9.3'],
['Sagar', 'SE', '1', '9.5'],
['Prateek', 'MCE', '3', '7.8'],
['Sahil', 'EP', '2', '9.1']]
with open('GFG', 'w') as f:
# using csv.writer method from CSV package
write = csv.writer(f)
write.writerow(fields)
write.writerows(rows)