-1

我想通过在出现的每个引号之后将它们拆分来将键中的值分组到值列表中。因此,每个地标都被分配了一个位置列表,例如:

{'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)

我得到错误->

数组的长度必须相同

我可以删除引号:

for v in data.values():
    if ('') in v:
        v.remove((''))

尽管如何在删除之前将引号之前的所有值分配为列表,以获得上面的预期值?

4

1 回答 1

2

这是拆分列表的一种方法''。它使用带有赋值表达式的短itertools.groupby列表理解python≥3.8

from itertools import groupby
d2 = d.copy()
d2['location'] = [G for _,g in groupby(d['location'], ''.__eq__)
                  if (G:=list(g)) != ['']]

python < 3.8的版本:

from itertools import groupby
d2 = d.copy()
d2['location'] = [G for _,g in groupby(d['location'], ''.__eq__)
                  for G in [list(g)] if G != ['']]

输出:

{'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']]}

作为数据框:

>>> pd.DataFrame(d2)
          Landmarks                                                     location
0     Great norwich                     [North 28th Street, Stadium High School]
1   Larger building                        [Charles Bridge, St Vitus' Cathedral]
2  Leaning building  [All Saints Church, Royal Courts of Justice, Lonsdale Road]

使用的输入:

d = {'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']}
于 2021-09-30T07:43:59.240 回答