How to output the table data read by the pandas module and the processed / created table data locally as a csv file.
to_csv ('file path', encording ='utf_8_sig', index = False)
└ ① "'File path'": Absolute path or relative path is acceptable
└ 2 "encoding ='utf_8_sig'": Character code specification
└ ③ "index = False": An instruction that does not include the index number column that is automatically generated when a table is read or created with pandas.
① is required. ② and ③ can be omitted.
** ▼ Example: When outputting the following table **
The variable df
contains the table below.
How to read csv file with python here
▼ Output as csv file
Illustration
df.to_csv("~/desktop/output.csv",index = False,encoding = "utf_8_sig")
** ▼ Details ** ①「df.to_csv」 └ Convert data df (variable) to csv file. ②「"~/desktop/output.csv"」 └ Specify the output destination with an absolute path. └ Output to the desktop with the file name "output.csv". └ Overwrite if the same file exists.
③「index = False」 └ No index number in the first column is required
④「encoding = "utf_8_sig"」 └ Character code has utf8 signature.
python
df.to_csv("~/desktop/output.csv",index = False)
error
df.to_csv("~/desktop/output.csv",index = False)
#output
# PermissionError: [Errno 13] Permission denied: 'C:\\Users\desktop/output.csv'
"Permission denied:" It looks like it was strongly denied, but it just couldn't be overwritten.
Recommended Posts