使用索引将Python列表转换为数据框

时间:2020-07-20 14:45:53

标签: python python-3.x pandas

我有一个庞大的python列表,其中包含许多格式的字符串

list = ['state1', 'town1','town2','town3', 'state2', 'town4', 'state3', 'town5','town6']

每个州的城镇数量都是可变的。

如何将其嵌套,看起来像这样:

list = [['state1', 'town1','town2','town3'], ['state2', 'town4'],['state3', 'town5','town6']

然后从那里将列表变成一个数据框,其中状态作为索引,城镇作为单个列?

2 个答案:

答案 0 :(得分:0)

让我们将列表作为:

lst = [['state', 'town','town','town'], ['state', 'town'],['state', 'town','town']]

要将其转换为状态为索引的数据框:

df=pd.DataFrame(lst).set_index(0, drop=True)

输出:

0        1       2       3
            
state   town    town    town
state   town    None    None
state   town    town    None

答案 1 :(得分:0)

因此,我们首先来看一些列表示例:

state_lst = ['California', 'New Mexico', 'Arizona', 'etc.']
state_town_lst = ['California', 'San Francisco', 'Los Angeles', 'San Diego', 'New Mexico', 'Albuquerque', 'Santa Fe', 'Arizona', 'Tucson']
town_lst =[]

因此,如您所见,加利福尼亚应该有3个城市,新墨西哥州应该有2个城市,亚利桑那州应该有1个城市。因此,我们遍历state_town_lst并检查项目是否出现在state_lst中。

for item in state_town_lst:
    if item in state_lst:
        state = item
        continue
    else:
        town = item
        
    town_item = (state, town)
    town_lst.append(town_item)
    
df = pd.DataFrame(town_lst, columns = ["State", "Town"])

这给您:

    State       Town
0   California  San Francisco
1   California  Los Angeles
2   California  San Diego
3   New Mexico  Albuquerque
4   New Mexico  Santa Fe
5   Arizona     Tucson