2017-06-06 96 views
1

我试图将元组列表转换为熊猫数据帧,但无法弄清楚如何实现。我的地址的结构如下所示:将元组列表转换为熊猫数据帧

addresses = [ 
[('the vicars inn', 'house'), ('68', 'house_number'), ('church lane', 'road'), ('arlesey', 'city'), ('beds', 'house')], 
[('the old oak', 'house'), ('85', 'house_number'), ('church lane', 'road'), ('arlesey', 'city'), ('beds', 'house')], 
[('adj', 'road'), ('85', 'house_number'), ('high street', 'road'), ('arlesey', 'city'), ('beds', 'house')], 
[('arlesey community centre', 'house'), ('high street', 'road'), ('arlesey', 'city'), ('beds', 'house')], 
[('arlesey community centre', 'house'), ('high street', 'road'), ('arlesey', 'city'), ('beds', 'house')] 
] 

理想情况下,我需要回到像一个数据帧:

 city   house    house_number  road 
0  arlesey  the vicars inn 68     church lane 
1  arlesey  the old oak  85     church lane 

我已经试过到目前为止转动表,但它不产生预期结果:

pd.DataFrame.from_records(addresses[0]).pivot(columns=1, values=0) 

有没有人有任何指导方法,我应该看看实现我理想的数据框?

山姆

+0

看来你在每个记录两套房子。你想保留哪一个? – Psidom

回答

2

您可以将每个记录转换为字典,然后使用DataFrame.from_records

pd.DataFrame.from_records([{k: v for v, k in row} for row in addresses]) 

#  city house house_number road 
#0 arlesey beds    68 church lane 
#1 arlesey beds    85 church lane 
#2 arlesey beds    85 high street 
#3 arlesey beds    NaN high street 
#4 arlesey beds    NaN high street