Writing Data Out
Saving a result, and the index that gets written with it.
to_csv writes the index too
By default the index becomes an unnamed first column. Reading that file back gives you a stray "Unnamed: 0", which is where most mysterious extra columns come from.
import pandas as pd
df = pd.DataFrame({"name": ["Ada"], "score": [92]})
df.to_csv("out.csv", index=False)
print(open("out.csv").read())
Write what you meant to write
columns= picks what goes out, and index=False keeps the row numbers out of it unless they mean something.
import pandas as pd
df = pd.DataFrame({"name": ["Ada"], "score": [92], "note": ["x"]})
df.to_csv("out.csv", index=False, columns=["name", "score"])
print(open("out.csv").read().strip())
Exercise
Try It YourselfWrite save_scores(df, path) that writes the table to path as CSV with no index column, then returns the first line of the file.
Press Run to see output