1
data = {'Sample':['S1', 'S1', 'S1' ,'S1' ,'S2' ,'S2' ,'S3' ,'S3', 'S4', 'Negative', 'Positive', 'Negative',
                 'S1', 'S1', 'S1' ,'S2' ,'S2' ,'S2' ,'S3' ,'S4', 'S4', 'Positive', 'Positive', 'Negative'], 
       'Location':['A1', 'A2', 'A3' ,'A4' ,'A5' ,'A6' ,'A7' ,'A8', 'A9', 'A10', 'A11', 'A12',
                   'B1', 'B2', 'B3' ,'B4' ,'B5' ,'B6' ,'B7' ,'B8', 'B9', 'B10', 'B11', 'B12'],
    'Repeat Number':['1', '2', '3' ,'4' ,'1' ,'2' ,'1' ,'2', '1', '1', '1', '2',
                  '1', '2', '3' ,'1' ,'2' ,'3' ,'1' ,'1', '2', '1', '2', '1',],
   'Identifier' :['asd01', 'asd02', 'asd03', 'asd04', 'asd05', 'asd06', 'asd07', 'asd08', 'asd09'
                 ,'asd10' ,'asd11' ,'asd12' ,'asd13' ,'asd14' ,'asd15', 'asd16', 'asd17', 'asd18',
                 'asd19', 'asd20', 'asd21', 'asd22', 'asd23', 'asd24']}

df1 = pd.DataFrame(data)

In the frame above, there are 4 S1 in location group A and they are repeats because they are in same location group A. For location B, there are 3 S1 and they are repeats because they are in same location group B. So they are given the repeat number(1,2,3,...).

For the example code above, I want to extract row for itself and its repeats when I give user input for 'Sample', 'Location'.

For instance, If I input Negative for 'Sample' and A for 'Location', Ideal result would look like this:

data = {'Sample':[ 'Negative', 'Negative'], 
       'Location':[ 'A10',  'A12'],
    'Repeat Number':[ '1', '2'],
   'Identifier' : ['asd10' ,'asd12']}

Also, I want to know how to only extract Identifier after the row selection.

I tried using df.loc[] but I dont know how to make user input on this since the inputs contain strings

4 Answers 4

3

Using below code, you would be able to extract data from dataframe:

sample = input('Enter Sample: ')
location = input('Enter Location: ')
df.loc[(df['Sample'] == sample) & (df['Location'].str.contains(location))]

This is output of above code:

Enter Sample: S2
Enter Location: B

    Sample  Location    Repeat Number   Identifier
15  S2  B4  1   asd16
16  S2  B5  2   asd17
17  S2  B6  3   asd18
Sign up to request clarification or add additional context in comments.

Comments

3

Just chain your conditions and use to_dict("list"):

print (df.loc[df["Sample"].eq("Negative")&df["Location"].str.contains("A")].to_dict("list"))

#{'Sample': ['Negative', 'Negative'], 'Location': ['A10', 'A12'], 'Repeat Number': ['1', '2'], 'Identifier': ['asd10', 'asd12']}

Comments

1

try this:

df[(df.Sample=='Negative') & (df.Location.str.startswith('A'))]

Comments

1

The following would work. I believe in this case str.startswith is better suited than str.contains:

import pandas as pd

data = {
    'Sample': [
        'S1', 'S1', 'S1', 'S1', 'S2', 'S2', 'S3', 'S3', 'S4', 'Negative', 'Positive', 'Negative',
        'S1', 'S1', 'S1', 'S2', 'S2', 'S2', 'S3', 'S4', 'S4', 'Positive', 'Positive', 'Negative'
    ],
    'Location': [
        'A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8', 'A9', 'A10', 'A11', 'A12',
        'B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'B9', 'B10', 'B11', 'B12'
    ],
    'Repeat Number': [
        '1', '2', '3', '4', '1', '2', '1', '2', '1', '1', '1', '2',
        '1', '2', '3', '1', '2', '3', '1', '1', '2', '1', '2', '1'
    ],
    'Identifier': [
        'asd01', 'asd02', 'asd03', 'asd04', 'asd05', 'asd06', 'asd07', 'asd08', 'asd09',
        'asd10', 'asd11', 'asd12', 'asd13', 'asd14', 'asd15', 'asd16', 'asd17', 'asd18',
        'asd19', 'asd20', 'asd21', 'asd22', 'asd23', 'asd24'
    ]
}


location_start = 'A'
sample_result = 'Negative'

df1 = pd.DataFrame(data)

# filter on the two criteria
df2 = df1[df1['Location'].str.startswith(location_start, na=False) & (df1['Sample'] == sample_result)]

print(df2)
      Sample Location Repeat Number Identifier
9   Negative      A10             1      asd10
11  Negative      A12             2      asd12

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.