I need to generate a csv using pandas to_csv function. I tried quote=csv.QUOTE_NONNUMERIC. But for one of the date time column I dont need double quotes.
Is there a way to select the columns for which we want double quotes?
I need to generate a csv using pandas to_csv function. I tried quote=csv.QUOTE_NONNUMERIC. But for one of the date time column I dont need double quotes.
Is there a way to select the columns for which we want double quotes?
Thanks people. I got the answer so just thought to share.
First I got the list of headers to be quoted and looped them something like below:
for col in quoteColumnsList:
df[col] = '"' + df[col] + '"'
Here my quotechar is '"'. Now I used to_csv with quote parameter as csv.QUOTE_NONE. This way we get the double quotes only to the required columns.
This worked better for me:
columnsList = data.columns #(or specific columns that you need)
for col in columnsList:
data[col] = data[col].apply(lambda x: "" + str(x) + "")
Example: For my project, I had to add quotes and convert them to list.
def convert_list(data):
columnsList = data.columns
for col in columnsList :
data[col] = data[col].apply(lambda x: "" + str(x) + "") #this adds single quotes without specifying it as "'")
df = data.values.tolist()
return df
© 2022 - 2024 — McMap. All rights reserved.