将行中的列表分成多个分块的行

时间:2019-10-15 15:02:40

标签: python-3.x pandas dataframe

我有一个熊猫数据框,其中某些行包含从系统返回的结果列表。我正在尝试将这些列表分成较小的块(在下面的可重现示例中为2个块),每个块作为新行。我确实做了工作,可以使用numpy的repeat函数复制行以为所需的每个块都添加一行,但是然后我不确定如何只在Result的位置包含列表的一部分。 (即一行应为['SUCCESS', 'Misc],下一个['Doom']与一行[['SUCCESS', 'Misc'],['Doom']]相对应)

我知道最好的解决方案是使用explode仅使列表中的每个项目成为新行,但是由于客户的要求,这不是一种选择。

代码

import pandas as pd
import numpy as np

data = {'Result': [['SUCCESS'], ['SUCCESS'], ['FAILURE'], ['Pending', 'Pending', 'SUCCESS', 'Misc', 'Doom'], ['FAILURE'], ['Pending', 'SUCCESS']], 'Date': ['10/10/2019', '10/09/2019', '10/08/2019', '10/07/2019', '10/06/2019', '10/05/2019']}
goal = {'Result': [['SUCCESS'], ['SUCCESS'], ['FAILURE'], ['Pending', 'Pending'], ['SUCCESS'], ['FAILURE'], ['Pending', 'SUCCESS']], 'Date': ['10/10/2019', '10/09/2019', '10/08/2019', '10/07/2019', '10/06/2019', '10/05/2019', '10/04/2019']}

df = pd.DataFrame(data)

df['len_res'] = df['Result'].str.len()

def chunking(l, n):
    for i in range(0, len(l), n):
        yield l[i:i + n]


df['chunks'] = 1
for i in range(len(df)):
    if df['len_res'][i] > 2:
        df['Result'][i] = list(chunking(df['Result'][i], 2))
        df['chunks'][i] = len(df['Result'][i])
    else:
        pass

实际输出

                                          Result        Date  len_res  chunks
0                                      [SUCCESS]  10/10/2019        1       1
1                                      [SUCCESS]  10/09/2019        1       1
2                                      [FAILURE]  10/08/2019        1       1
3  [[Pending, Pending], [SUCCESS, Misc], [Doom]]  10/07/2019        5       3
4                                      [FAILURE]  10/06/2019        1       1
5                             [Pending, SUCCESS]  10/05/2019        2       1

所需的输出

                                          Result        Date  len_res  chunks
0                                      [SUCCESS]  10/10/2019        1       1
1                                      [SUCCESS]  10/09/2019        1       1
2                                      [FAILURE]  10/08/2019        1       1
3                             [Pending, Pending]  10/07/2019        5       3
4                                [SUCCESS, Misc]  10/07/2019        5       3
5                                         [Doom]  10/07/2019        5       3
6                                      [FAILURE]  10/06/2019        1       1
7                             [Pending, SUCCESS]  10/05/2019        2       1

使用 np.repeat

df = df.loc[np.repeat(df.index.values, df.chunks)]
df = df.reset_index(drop=True)

                                          Result        Date  len_res  chunks
0                                      [SUCCESS]  10/10/2019        1       1
1                                      [SUCCESS]  10/09/2019        1       1
2                                      [FAILURE]  10/08/2019        1       1
3  [[Pending, Pending], [SUCCESS, Misc], [Doom]]  10/07/2019        5       3
4  [[Pending, Pending], [SUCCESS, Misc], [Doom]]  10/07/2019        5       3
5  [[Pending, Pending], [SUCCESS, Misc], [Doom]]  10/07/2019        5       3
6                                      [FAILURE]  10/06/2019        1       1
7                             [Pending, SUCCESS]  10/05/2019        2       1

1 个答案:

答案 0 :(得分:1)

如果您使用的是熊猫v0.25或更高版本,请使用explode

size = 2
df['Result'] = df['Result'].apply(lambda r: np.array_split(r, np.ceil(len(r) / size)))
df['chunks'] = df['Result'].str.len()

df = df.explode('Result')

np.array_split将数组分成n = ceil(len(r) / size)个部分:

[1]     --> [[1]]
[1,2]   --> [[1,2]]
[1,2,3] --> [[1,2], [3]]

explodeResult中数组最外层的每个元素重复每一行。