I want to print a dictionary into a csv file. Dictionary is containing a subject code as a key and a mark that one student got as a value.
Here is my .py code:
import pandas as pd
classes = dict()
print('Type STOP to end.\n')
while True:
key = input('Subject ID: ')
if key == 'STOP':
break
else:
value = input('Mark: ')
classes[key] = value
print(classes)
df = pd.DataFrame.from_dict(classes, orient='index')
df.to_csv("student_report.csv")
Code prints the dict:
{'IT255': '10', 'IT335': '9', 'CS324': '10', 'CS225': '7'}
Here is csv file:
,0
IT255,10
IT335,9
CS324,10
CS225,7
I don't know what is causing the ,0 at the top. Any help?
UPDATE
Printing the data frame brings this:
0
IT255 10
IT335 9
CS324 10
CS225 7
CodePudding user response:
Pandas insists that each column have a title, and it wants to write those column names as the CSV header. Since you didn't provide any column names in your import, pandas assigns names to the columns. The first column is an index, which doesn't need a name. The other columns are assigned names starting with 0. You can supply your own column names with the header
parameter to to_csv
, or pass header=False
to suppress them.
As a side note, if this is ALL you are using pandas
for, it would be MUCH more efficient to use the Python csv
module. pandas
is a very large module that can take multiple second to load. If you don't need its fancy capabilities, just use csv
.