[英]Python,using dataframes how to split the value of a string in a column and then add a new column with the value from the split
好。 这个标题很糟糕,但让我们尝试一个例子。 让我们想象我们有这样的事情:
c1 c2
column1-x 2
column1-y 3
column2-x 5
column3 6
我希望它最终像这样:
c1 c2 c3
column1-x 2 column1
column1-y 3 column1
column2-x 5 column2
column3 6 column3
您可能已经猜到这是一次热编码后重新加入值重要性的尝试,此后,我将在c3中进行所有具有相同值的值的总和,但是为此,我需要能够在c1之前检查值“-”添加第三列c3。
也许您正在寻找str.split
df['c3'] = df.c1.str.split('-').str[0]
df
c1 c2 c3
0 column1-x 2 column1
1 column1-y 3 column1
2 column2-x 5 column2
3 column3 6 column3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.