首页 > 解决方案 > 如何分隔数据框中的两行

问题描述

我有一张多伦多社区的表格,在某些情况下,您会在同一个盒子中找到两个或三个社区,所以同一行(对于同一个自治市镇)我想将它们分成两行如何做请!我正在使用熊猫。谢谢

标签: pythonpandas

解决方案


你应该用你预期的输入和输出的例子来充实这个问题,但我认为你想要这样的东西使用pandas.DataFrame.explode

import pandas as pd

df = pd.DataFrame(dict(
    city=['Seattle'],
    hood=[['SoDo', 'Maple Leaf', 'View Ridge', 'North Gate', 'SLU']],
))

print('df before exploding:\n', df)

df = df.explode('hood')

print('\ndf after exploding:\n', df)

输出:

df before exploding:
       city                                             hood
0  Seattle  [SoDo, Maple Leaf, View Ridge, North Gate, SLU]

df after exploding:
       city        hood
0  Seattle        SoDo
0  Seattle  Maple Leaf
0  Seattle  View Ridge
0  Seattle  North Gate
0  Seattle         SLU

Process finished with exit code 0

编辑:OP回应:

是的,这正是我想要的,但我有一个问题 m 盒子是这样的:df before exploding: hood =[' SoDo, Maple Leaf, View Ridge, North Gate, SLU'] 所以我尝试使用 explod 但它不是可能是因为 , 是一个字符串字符

如果是这种情况并且它只是一个字符串,那么还有以下额外步骤:

import pandas as pd

df = pd.DataFrame(dict(
    city=['Seattle'],
    hood=[['SoDo, Maple Leaf, View Ridge, North Gate, SLU']],
))
print('df before exploding:\n', df)

df['hood'] = df['hood'].apply(lambda x: x[0].split(', '))
print('\ndf after splitting string in list:\n',df)

df = df.explode('hood')
print('\ndf after exploding:\n', df)

输出:

df before exploding:
       city                                             hood
0  Seattle  [SoDo, Maple Leaf, View Ridge, North Gate, SLU]

df after splitting string in list:
       city                                             hood
0  Seattle  [SoDo, Maple Leaf, View Ridge, North Gate, SLU]

df after exploding:
       city        hood
0  Seattle        SoDo
0  Seattle  Maple Leaf
0  Seattle  View Ridge
0  Seattle  North Gate
0  Seattle         SLU

Process finished with exit code 0


推荐阅读