Nono_sad
Nono_sad

Reputation: 443

Split value present in one Column by specific character

I have a Data-frame which contain two column.

On the first column (Motif_name) my value look like that :

Motif_Name_xx/Description/Homer

Second column just contain a score.

I'm trying to split my first column by '/' and conserve the first element.

Basically what I tried :

   df=df['Motif_name'].str.split('/').str[1]

Here the problem is that my data-frame is replaced :

   print(df)
   0      Motif_1
   1      Motif_2

I lost the header and the second column...

I expect to have a data-frame like that :

   Motif_name       Score
 0 Motif_Name_xx1  0.001
 1 Motif_Name_xx2  0.05
 2 Motif_Name_xx3  0.02
 3 Motif_Name_xx4  0.01

Upvotes: 0

Views: 452

Answers (2)

Nono_sad
Nono_sad

Reputation: 443

Ok I just see the solution when I was editing my question, so if someone else need the answer :

 EF1a_R1_df['Motif_name']=EF1a_R1_df['Motif_name'].str.split('/').str[0]

Basically, in place to replace the all data-frame, just replace the column and it work well.

Upvotes: 1

jezrael
jezrael

Reputation: 862611

It seems need parameter n=1 for split by first / and str[0] for get first value of lists (python count from 0) and then assign it to same column:

df['Motif_name'] = df['Motif_name'].str.split('/', n=1).str[0]

Upvotes: 2

Related Questions