Commit f73d0edf authored by Cecilia Nievas's avatar Cecilia Nievas
Browse files

Changed method to write DataFrame to CSV file

parent 5cffe0e6
Pipeline #47718 passed with stage
in 2 minutes and 44 seconds
......@@ -55,36 +55,22 @@ def write_CSV_file(in_df, cols_order, filename, writemode="a", sep=","):
else:
write_header = True
# Open the CSV file
out_csv = open(filename, writemode)
# Ignore elements of 'cols_order' that are not columns of in_df
cols_order_sanitised = deepcopy(cols_order)
for colname in cols_order:
if colname not in in_df.columns:
cols_order_sanitised.remove(colname)
# Write the header
if write_header:
out_csv.write("%s\n" % (sep.join(cols_order_sanitised)))
# Write the contents (row by row)
for i in range(in_df.shape[0]):
out_str = []
for colname in cols_order_sanitised:
if isinstance(in_df[colname].values[i], str):
formatted_value = deepcopy(in_df[colname].values[i])
elif isinstance(in_df[colname].values[i], float):
formatted_value = "{:.5f}".format(in_df[colname].values[i])
else:
formatted_value = str(in_df[colname].values[i])
out_str.append(formatted_value)
out_csv.write("%s\n" % (sep.join(out_str)))
# Close the CSV file
out_csv.close()
# Write to CSV
in_df.to_csv(
filename,
header=write_header,
columns=cols_order_sanitised,
index=False,
mode=writemode,
float_format="%.5f",
sep=sep,
)
return
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment