【发布时间】:2021-10-17 16:01:12
【问题描述】:
假设我们有一个变量 foo = "text" 。您将如何将其转换为 foo = '"text"' ?有没有办法在不使用库 urllib2 的情况下做到这一点?
编辑:下面是一个脚本:
def most_common(lst):
if(len(lst) > 0):
return max(set(lst), key=lst.count)
else:
return 0
test_list = list(df['a'])) # a is a column that can take the the values '"apple"', '"pear"', '"carrot"'
test_list_most = most_common(test_list) # returns "apple"
但假设我们要过滤数据框:
df = len(df[df['a'] == test_list_most].index) # length would be 0
这就是这个问题的上下文以及为什么我们要在双引号周围添加单引号。
【问题讨论】:
-
urllib2的作用是什么? -
foo = "text"是 4 个字母t e x t。你想要六个字母" t e x t "?foo = '"text"'怎么还不是答案? -
>>> foo="'\"test\"'" >>> print(foo) '"test"' -
我认为答案是通过从 a 列中删除那些额外的引号来清理数据框:
df['a'] = df['a'].str.slice(1, -1)。我还想知道 a 列是否真的有这些 qoutes,或者这是否只是熊猫在显示数据框时使用的repr表示。print(df['a'][0])是否显示引号?
标签: python-3.x string concatenation