1

Hello I have a df such as :

COL1 COL2
A g1
B g1.t1
C transcript_id "g1.t1"; gene_id "g1"
D g2
E g2.t1
F transcript_id "g2.t1"; gene_id "g2"
G transcript_id "g2.t1"; gene_id "g2"

and I would like to add a new COL3 where I only put gvalue for each row

Here I should get :

COL1 COL2                               COL3
A g1                                    g1
B g1.t1                                 g1
C transcript_id "g1.t1"; gene_id "g1"   g1
D g2                                    g2
E g2.t1                                 g2
F transcript_id "g2.t1"; gene_id "g2"   g2
G transcript_id "g2.t1"; gene_id "g2"   g2

I tought I could use something like re.sub ?

I tried :

table[COL3]= re.sub(r'(?<=transcript_id )*.+(?<=gene_id ")','',table[COL2])

1 Answer 1

2

Is it:

df['COL3'] = df.COL2.str.extract('(g\d+)')

Output:

  COL1                                 COL2 COL3
0    A                                   g1   g1
1    B                                g1.t1   g1
2    C  transcript_id "g1.t1"; gene_id "g1"   g1
3    D                                   g2   g2
4    E                                g2.t1   g2
5    F  transcript_id "g2.t1"; gene_id "g2"   g2
6    G  transcript_id "g2.t1"; gene_id "g2"   g2
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.