Jason Smith
Jason Smith

Reputation: 127

Insert quotes in field

I am sorry if this question may sound stupid. I am merging two csv files and the merged csv file has a field that contains commas with values within that field. How can I insert quotes around all the values in that specific field?

import pandas as pd

first = pd.read_csv('C:/datainput')
first['project_id'] = 2
second = pd.read_csv('C:/datainputting')
second['project_id'] = 1

merged = pd.concat([first, second], ignore_index = True)
getting_away = merged.to_csv('C:/datainputcombination.csv', index=False)

for row in getting_away:
    row[23] = "'%s'" % row[23]

print "Data Input all ready to fire"

Error:

Traceback (most recent call last):
  File "C:/Users/datainputcombo.py", line 14, in <module>
    for row in getting_away:
TypeError: 'NoneType' object is not iterable

Row [23]:

AGG Development, LLC
RRR Regional, PLLC
RJH Developers, LLC
....................

Upvotes: 0

Views: 40

Answers (2)

Alex G Rice
Alex G Rice

Reputation: 1579

If you are running into problems with comma characters downstream from writing the csv file, you can use the this parameter to turn on quoting:

merged.to_csv('C:/datainputcombination.csv', quoting=csv.QUOTE_NONNUMERIC)

Upvotes: 1

Bill Bell
Bill Bell

Reputation: 21643

Here's simpler code that demonstrates what's happening.

>>> import pandas as pd
>>> df = pd.DataFrame({'a':[1,2,3,4],'b':[4,5,6,7]})
>>> getting_away = df.to_csv('aFile.csv')
>>> getting_away
>>> 

I make a simple dataframe, then write it to a csv as you did using .to_csv. The result of this method is stored in getting_away but, as you see, it's None. Which is to say that .to_csv returns nothing, which makes good sense because it has put its results in a file.

Upvotes: 2

Related Questions