简体   繁体   中英

How to convert column with list of values into rows in Pandas DataFrame including previous column also

Hi I have a dataframe like this:

    A             B 
0:  a           [[L1, L2]]
1:  b           [[L1, L2, L3]]

I want to change it into:

    A     B                 C 
0:  a    [[L1, L2]]         L1
1:  a    [[L1, L2]]         L2
2:  b    [[L1, L2, L3]]     L1
3:  b    [[L1, L2, L3]]     L2
4:  b    [[L1, L2, L3]]     L3

How can I do that?

try some like this :

import pandas as pd
from io import StringIO
data = """
A B
a [[L1,L2]]
b [[L1,L2,L3]]
"""
df = pd.read_csv(StringIO(data),sep=' ')
df['C']=df['B']
df['C']=df.C.astype(str).replace(['\[','\]', "'", "\s+"], '', regex=True)
print(df.set_index(df.columns.drop('C',1).tolist()).C.str.split(',', expand=True).stack().reset_index().rename(columns={0:'C'}).loc[:, df.columns])

result :

   A             B   C
0  a     [[L1,L2]]  L1
1  a     [[L1,L2]]  L2
2  b  [[L1,L2,L3]]  L1
3  b  [[L1,L2,L3]]  L2
4  b  [[L1,L2,L3]]  L3

One solution using itertools.chain :

import pandas as pd
from itertools import chain

# old dataframe:
df = pd.DataFrame({'A': ['a', 'b'],
                   'B': [  [['L1', 'L2']], [['L1', 'L2', 'L3']]   ]})

d = {'A':[], 'B':[], 'C': []}
for a, b in zip(df['A'], df['B']):
    for c in chain.from_iterable(b):
        d['A'].append(a)
        d['B'].append(b)
        d['C'].append(c)

# new dataframe:     
df = pd.DataFrame(d)
print(df)

Prints:

   A               B   C
0  a      [[L1, L2]]  L1
1  a      [[L1, L2]]  L2
2  b  [[L1, L2, L3]]  L1
3  b  [[L1, L2, L3]]  L2
4  b  [[L1, L2, L3]]  L3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM