Home > Enterprise >  How can we add quotes around a string in a column in a data frame
How can we add quotes around a string in a column in a data frame

Time:12-30

I have a field named 'location'; it looks like this.

3200 PROVIDENCE DRIVE ANCHORAGE 99508
2500 SOUTH WOODWORTH LOOP PALMER 99645
3260 HOSPITAL DR JUNEAU 99801
1650 COWLES STREET FAIRBANKS 99701
2801 DEBARR ROAD ANCHORAGE 99508

I am trying to add double quotes around the string, so it looks like this.

"3200 PROVIDENCE DRIVE ANCHORAGE 99508"
"2500 SOUTH WOODWORTH LOOP PALMER 99645"
"3260 HOSPITAL DR JUNEAU 99801"
"1650 COWLES STREET FAIRBANKS 99701"
"2801 DEBARR ROAD ANCHORAGE 99508" 

I tried this (it does something very weird):

for col in results:
    results['location'] =  results['location'].apply(lambda x: """   str(x)   """)

I also tried this (it adds way too many quotes):

for col in results:
    results['location'] = '"'   results['location']   '"'

Thoughts?

CodePudding user response:

@ASH, try this:

words = ['3200 PROVIDENCE DRIVE ANCHORAGE 99508',
'2500 SOUTH WOODWORTH LOOP PALMER 99645',
'3260 HOSPITAL DR JUNEAU 99801',
'1650 COWLES STREET FAIRBANKS 99701',
'2801 DEBARR ROAD ANCHORAGE 99508']

['"' str(x) '"' for x in words]

Output:

['"3200 PROVIDENCE DRIVE ANCHORAGE 99508"',
 '"2500 SOUTH WOODWORTH LOOP PALMER 99645"',
 '"3260 HOSPITAL DR JUNEAU 99801"',
 '"1650 COWLES STREET FAIRBANKS 99701"',
 '"2801 DEBARR ROAD ANCHORAGE 99508"']

CodePudding user response:

You could use an f-string in the lamba.

results['location'] = results['location'].apply(lambda x: f'"{x}"')
  • Related