[英]Split the single column to 4 different columns in Dataframe
I just need need to split a single column of dataframe to 4 different columns.我只需要将 dataframe 的一列拆分为 4 个不同的列。 I tried few steps but didn't worked.我尝试了几个步骤但没有奏效。
DATA1:数据 1:
Dump
12525 2 153898 Winch
24798 1 147654 Gear
65116 4 Screw
46456 1 Rowing
46563 5 Nut
Expected1:预期1:
Item Qty Part_no Description
12525 2 153898 Winch
24798 1 147654 Gear
65116 4 Screw
46456 1 Rowing
46563 5 Nut
DATA2:数据2:
Dump
12525 2 153898 Winch Gear
24798 1 147654 Gear nuts
65116 X Screw bolts
46456 1 Rowing rings
46563 X Nut
Expected2:预期2:
Item Qty Part_no Description
12525 2 153898 Winch Gear
24798 1 147654 Gear nuts
65116 X Screw bolts
46456 1 Rowing rings
46563 X Nut
I tried the below code我试过下面的代码
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
Any suggestions, how can i fix this???任何建议,我该如何解决???
Use str.extract
:使用str.extract
:
data_df[['Item','Qty','Part_no','Description']] = \
data_df['Dump'].str.extract(r'(\d+)\s+(\d+)\s+(\d*)\s*(\w+)')
Output: Output:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.