我有一个unicode字符串[u'1,2,3,4']
,想要将其转换为[1,2,3,4]
。
例子:
a=[u'1,2,3']
我是从url值中获得的,我必须将此格式转换为:
a= [1,2,3]
是否可以比较此列表值存在于熊猫数据框中的特定列中?
假设您有数组[1, 2, 3]
,这是您可以测试DataFrame是否包含值的方法
>>> df = pd.DataFrame({'a': [1, 3, 5, 7, 9]})
>>> a = [1, 2, 3]
>>> for x in a:
... print '{x} is in df'.format(x=x), any(df.a == x)
...
1 is in df True
2 is in df False
3 is in df True
我正在使用df.<column_name> == <test_value>
,并将其包装在any()
如果找到至少一个匹配项的情况下返回true
我在处分割了字符串,,
然后将结果转换为int
>>> [int(x) for x in [u'1,2,3'][0].split(',')]
[1, 2, 3]
解释
>>> a=[u'1,2,3']
>>> a
[u'1,2,3'] # This a list with one string: '1,2,3'
>>> a = a[0]
>>> a
u'1,2,3' # I get the string from the list above
>>> a.split(',') # I split this string on ','
[u'1', u'2', u'3'] # this is still a list of strings,
# so I run int(x) on each element
# using a list comprehension below
>>> [int(x) for x in [u'1,2,3'][0].split(',')]
[1, 2, 3] # the result you want
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句