select data based on a string criteria python -


i have large database in want select columns meet criteria:

my data looks following:

name   b  c  target-01  5196     24     24   target-02  5950    150    150  target-03  5598     50     50  object-01  6558     44     -1  object-02  6190     60     60  

i want select data name starts target.

so selected df be:

target-01  5196     24     24   target-02  5950    150    150  target-03  5598     50     50  

i reading data using:

data = pd.read_csv('catalog.txt', sep = '\s+', header = none, skiprows =1 ) 

how can select data want?

use str.startswith , boolean indexing:

print (df[df.name.str.startswith('target')])         name        b    c 0  target-01  5196   24   24 1  target-02  5950  150  150 2  target-03  5598   50   50 

another solution str.contains:

print (df[df.name.str.contains(r'^target')])         name        b    c 0  target-01  5196   24   24 1  target-02  5950  150  150 2  target-03  5598   50   50 

last solution filter:

df.set_index('name', inplace=true)  print (df.filter(regex=r'^target', axis=0))                  b    c name                      target-01  5196   24   24 target-02  5950  150  150 target-03  5598   50   50  print (df.filter(regex=r'^target', axis=0).reset_index())         name        b    c 0  target-01  5196   24   24 1  target-02  5950  150  150 2  target-03  5598   50   50 

Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -