Python

Python,第1张

Python

这将座垫按空间划分,并给每个单独的行。

In [43]: dfOut[43]:    CustNum     CustomerName  ItemQty Item      Seatblocks  ItemExt0    32363  McCartney, Paul        3  F04    2:218:10:4,6       601    31316     Lennon, John       25  F01  1:13:36:1,12 1:13:37:1,13      300In [44]: s = df['Seatblocks'].str.split(' ').apply(Series, 1).stack()In [45]: s.index = s.index.droplevel(-1) # to line up with df's indexIn [46]: s.name = 'Seatblocks' # needs a name to joinIn [47]: sOut[47]: 0    2:218:10:4,61    1:13:36:1,121    1:13:37:1,13Name: Seatblocks, dtype: objectIn [48]: del df['Seatblocks']In [49]: df.join(s)Out[49]:    CustNum     CustomerName  ItemQty Item  ItemExt    Seatblocks0    32363  McCartney, Paul        3  F04       60  2:218:10:4,61    31316     Lennon, John       25  F01      300  1:13:36:1,121    31316     Lennon, John       25  F01      300  1:13:37:1,13

或者,将每个冒号分隔的字符串放在自己的列中:

In [50]: df.join(s.apply(lambda x: Series(x.split(':'))))Out[50]:    CustNum     CustomerName  ItemQty Item  ItemExt  0    1   2     30    32363  McCartney, Paul        3  F04       60  2  218  10   4,61    31316     Lennon, John       25  F01      300  1   13  36  1,121    31316     Lennon, John       25  F01      300  1   13  37  1,13

这有点丑陋,但也许有人会采用更漂亮的解决方案。



欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/4946190.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-13
下一篇2022-11-13

发表评论

登录后才能评论

评论列表(0条)

    保存