Hemantchandru naik
Hemantchandru naik

Reputation: 101

Pandas: Getting new dataframe from existing dataframe from list of substring present in column name

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

Upvotes: 2

Views: 27

Answers (1)

BENY
BENY

Reputation: 323226

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

Upvotes: 2

Related Questions