jenkelblankel jenkelblankel - 1 year ago 57
Python Question

Insert duplicate value into Pandas row

I want to split up the Jobs, Steve. 01/31 column so that [SPGC-9456, 6.0]] is on its own row.

What my code outputs now:

2017-01-31 2017-02-01
Gates, Bill. [[SPGC-14075, 0.5]] NaN
Jobs, Steve. [[SPGC-14075, 3.5], [SPGC-9456, 6.0]] NaN
White, John ANDERSON. [[SPGC-14075, 1.75]] [[SPGC-9456, 1.75]]


What I want:

2017-01-31 2017-02-01
Gates, Bill. [[SPGC-14075, 0.5]] NaN
Jobs, Steve. [[SPGC-14075, 3.5] NaN
Jobs, Steve. [SPGC-9456, 6.0]] NaN
White, John ANDERSON. [[SPGC-14075, 1.75]] [[SPGC-9456, 1.75]]

Answer Source
col = '2017-01-31'
v = df[col].values.tolist()
l = [len(x) for x in v]
d = {col: [[x] for y in v for x in y]}
df.reindex(df.index.repeat(l)).assign(**d)

                                 2017-01-31           2017-02-01
Gates, Bill.            [[SPGC-14075, 0.5]]                  NaN
Jobs, Steve.            [[SPGC-14075, 3.5]]                  NaN
Jobs, Steve.             [[SPGC-9456, 6.0]]                  NaN
White, John ANDERSON.  [[SPGC-14075, 1.75]]  [[SPGC-9456, 1.75]]

Setup

df = pd.DataFrame([
        [[['SPGC-14075', .5]], np.nan],
        [[['SPGC-14075', 3.5], ['SPGC-9456', 6.]], np.nan],
        [[['SPGC-14075', 1.75]], [['SPGC-9456', 1.75]]]
    ], 
    'Gates, Bill.|Jobs, Steve.|White, John ANDERSON.'.split('|'),
    ['2017-01-31', '2017-02-01']
)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download