BryceSoker
BryceSoker

Reputation: 644

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

Ok. That title is terrible but let's try with an example. Let us imagine we have soemthing like this:

c1         c2   
column1-x   2  
column1-y   3  
column2-x   5
column3     6

And i would want it to end up like this:

c1         c2   c3
column1-x   2   column1
column1-y   3   column1
column2-x   5   column2
column3     6   column3

As you probably guessed this is an attempt on rejoining the value importance after one-hot encoding after this I will make the sum of all values with the same value in c3, but for that i need to be able to check the value in c1 before the "-" to add that third column c3.

Upvotes: 1

Views: 37

Answers (1)

cs95
cs95

Reputation: 402353

Maybe you're looking for 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

Upvotes: 2

Related Questions