roschach
roschach

Reputation: 9336

Pandas avoid double quotes in converted numeric column with appended comma

Suppose I want to modify a column value of a Pandas dataframe by adding a closing square bracket and a comma, i.e., ],. The column is of float type.

I am applying a custom function to the column with:

    df['colName'] = df['colName'].apply(parentisi2)

When I add just the square bracket, i.e.,

def parentisi2(name):
    return str(name)+"]"

everything is fine.

However when I add the ,, i.e.,

def parentisi2(name):
    return str(name)+"],"

when saving to file also double quotes are inserted. For example instead of having 9.336669722189999], I get "9.336669722189999],"

How can I avoid that?

Upvotes: 1

Views: 392

Answers (1)

EDIT: I had misread OP's intention. This answer handles the separator in the csv export.

It works if you use another separator. Below I have used ;

import pandas as pd


df = pd.DataFrame({'colName': [0.1, 0.2]})

def parentisi2(name):
    return str(name)+"],"

df['parentisi'] = df['colName'].apply(lambda x: parentisi2(x))
df['parentisi'] = df['parentisi'].astype(str)
df.to_csv('parentisi.csv',
          sep = ';')

Upvotes: 1

Related Questions