简体   繁体   English

我们如何在数据框中的列中的字符串周围添加引号

[英]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.

相关问题 在数据框列中的字符串周围添加双引号 - To add double quotes around the strings in a data frame column 我们如何在数据框中的特定列中找到最后一行? - How can we find the last row, in a specific column, in a data frame? 我们如何计算数据框列中的重复数据并将结果分配给同一数据框中的新列? - How can we count dupes in a column of a data frame and assign the results to a new column in the same data frame? 当大小/索引不同时,我们如何从另一个数据帧将字段添加到数据帧? - How can we add a field to a data frame, from another data frame, when the sizes/indexes are different? 如何在数据框中添加列? - How to add column to a data frame? 如何将列的一部分添加到新的 Pandas 数据框中? - How can I add parts of a column to a new pandas data frame? 我们如何处理大约 300K 列中存在的大量类别。 在预处理数据? - How we can deal with large number of category present in a column around 300K. in Preprocessing Data? 我们如何计算数据框中的项目并将结果分配给数据框中的新列? - How can we do counts of items in a data frame and asign results to a new column in the dataframe? 如何在python中删除字符串周围的引号? - How to remove quotes around a string in python? 如何用括号替换字符串周围的引号? - How to replace quotes around string with brackets?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM