【问题标题】:Split the single column to 4 different columns in Dataframe将单列拆分为 Dataframe 中的 4 个不同的列
【发布时间】:2022-11-23 22:29:26
【问题描述】:

我只需要将一列数据框拆分为 4 个不同的列。我尝试了几个步骤但没有奏效。

数据:

 Dump               
12525 2 153898 Winch
24798 1 147654 Gear
65116 4        Screw 
46456 1        Rowing
46563 5        Nut       

预期的:

 Item  Qty  Part_no  Description             
12525  2    153898   Winch
24798  1    147654   Gear
65116  4             Screw 
46456  1             Rowing
46563  5             Nut       

我试过下面的代码

data_df[['Item','Qty','Part_no','Description']] = data_df["Dump"].str.split(" ", 3, expand=True)

and got the output like 

 Item  Qty  Part_no  Description             
12525  2    153898   Winch
24798  1    147654   Gear
65116  4    Screw 
46456  1    Rowing
46563  5    Nut       

任何建议,我该如何解决???

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    使用str.extract

    data_df[['Item','Qty','Part_no','Description']] = 
    data_df['Dump'].str.extract(r'(d+)s+(d+)s+(d*)s*(w+)')
    

    输出:

                        Dump   Item Qty Part_no Description
    0   12525 2 153898 Winch  12525   2  153898       Winch
    1    24798 1 147654 Gear  24798   1  147654        Gear
    2   65116 4        Screw  65116   4               Screw
    3  46456 1        Rowing  46456   1              Rowing
    4     46563 5        Nut  46563   5                 Nut
    

    【讨论】:

      猜你喜欢
      • 2022-11-23
      • 2022-12-15
      • 2018-06-25
      • 2022-01-03
      • 2020-09-13
      • 2019-04-05
      • 1970-01-01
      • 2021-10-12
      • 1970-01-01
      相关资源
      最近更新 更多