我有一个数据帧df
,其中一列名为columnList
作为str
"1 2,7 8,10 7"
然后我将它们转换为一个列表,如图所示:
[1 2,7 8,10 7]
我想将列表中的值转换为元组:
[(1,2),(7,8),(10,7)]
当前代码:
temp = df['columnList'].str.split(',')
result = list(zip(temp[::2], temp[1::2]))
print(result)
我得到了一张空名单
df
看起来像这样:
column1 columnList
YY 1 2,7 8,10 7
名称:df,数据类型:object
您可以在拆分字符后将其映射为整数,然后将映射对象转换为元组:
这里不需要使用
zip
,只需迭代列表,拆分每个元素并将其存储为元组试试这个
产出
相关问题 更多 >
编程相关推荐