我想将键中的值分组到一个值列表中,方法是在出现的每个引号后将它们拆分。所以每个地标都有一个位置列表, 例如:
{'Landmarks': ['Great norwich', 'Larger building', 'Leaning building'],
'location': ['North 28th Street',
'Stadium High School',
'',
'Charles Bridge',
"St Vitus' Cathedral",
'',
'All Saints Church',
'Royal Courts of Justice',
'Lonsdale Road']
}
预期产出:
作为字典:
{'Landmarks': ['Great norwich', 'Larger building', 'Leaning building'],
'location': [['North 28th Street', 'Stadium High School'],
['Charles Bridge', "St Vitus' Cathedral"],
['All Saints Church', 'Royal Courts of Justice', 'Lonsdale Road']]
}
或作为数据帧:
Landmarks Locations
Great norwich ['North 28th Street','Stadium High School']
Larger building ['Charles Bridge','St Vitus' Cathedral',]
Leaning building ['All Saints Church','Royal Courts of Justice','Lonsdale Road']
我试过:
pd.DataFrame(data)
我得到错误->
arrays must all be same length
我可以使用以下命令删除引号:
for v in data.values():
if ('') in v:
v.remove((''))
但是,如何在删除前将引号之前的所有值作为列表分配,以获得上面的预期值
下面是一种在
''
上拆分列表的方法。它使用了一个简短的列表理解itertools.groupby
和一个assignment expression(python≥ 3.8)python版本<;3.8:
输出:
作为数据帧:
使用的输入:
相关问题 更多 >
编程相关推荐