如何在Python中扩展行中的多个列表列

问题描述 投票:0回答:1

像这样的数据框

身份证 MS DS
654 1500,10000,20000,30000 60,365,730
131 1500,10000,20000 60,365,730
598 1500,10000,20000,30000 60,365,730

输出为

身份证 MS DS
654 1500 60
654 10000 365
654 20000 730
654 30000
131 1500 60
131 10000 365
131 20000 730
598 1500 60

尝试这样做,但不起作用,因为它将列表更改为这样的字符串 [('1500,10000,20000,30000','60,365,730')],

[('1500,10000,20000','60,365,730')],

from itertools import zip_longest
df.apply(lambda row: list(zip_longest(row['MS'], 
                                  row['DS'], 
                                  fillvalue = 'Nan')),axis = 1)
python dataframe list expand melt
1个回答
0
投票

您可以使用:

df[["MS","DS"]] = df[["MS","DS"]].apply(lambda x: x.str.split(","),axis=1)
for i in ["MS","DS"]:
    df = df.explode(i)
© www.soinside.com 2019 - 2024. All rights reserved.