2

Hello I have dataframe called df and list of substring present in dataframe main problem i am facing is some of the substrings are not present in dataframe.

    ls = ["SRR123", "SRR154", "SRR655", "SRR224","SRR661"]
    
    data = {'SRR123_em1': [1,2,3], 'SRR123_em2': [4,5,6], 'SRR661_em1': [7,8,9], 'SRR661_em2': [6,7,8],'SRR453_em2': [10,11,12]}
    
    df = pd.DataFrame(data)

Output:

  SRR123_em1  SRR123_em2    SRR661_em1     SRR661_em2
      1           4            7              6
      2           5            8              7 
      3           6            9              8

please any one suggest me how can obtaine my output

1 Answer 1

2

Do filter with str.contains

sub_df=df.loc[:,df.columns.str.contains('|'.join(ls))].copy()
Out[295]: 
   SRR123_em1  SRR123_em2  SRR661_em1  SRR661_em2
0           1           4           7           6
1           2           5           8           7
2           3           6           9           8
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.