[英]How can we add quotes around a string in a column in a data frame
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?想法?
@ASH, try this: @ASH,试试这个:
['"'+str(x)+'"' for x in words]
Output: 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"']
You could use an f-string in the lamba.您可以在 Lamba 中使用 f 字符串。
for col in results:
results['location'] = results['location'].apply(lambda x: f'"{x}"')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.