简体   繁体   中英

How to get a value from an excel cell if another cell matches a string from a list

I have the following table:

( https://i.stack.imgur.com/nV4RA.png )

And the following list:

( https://i.stack.imgur.com/0SGyO.png )

What I wish to do is to extract the information from the Cell type, treatment, field 1 and field 2 columns when there is a match between the Location column in my table and the string in my list.

What I expect to obtain is for example: if r01c01 from the table [in the column Location] is present in my string list, then get a variable for each individual information like this:

cell_type=HCT WT

treatment=NT

field_1=25

field_2=34

I have tried the following code just to obtain the information from Field 1, however I am getting the following error: InvalidIndexError: ([], 'Field 1')

for i in range(len(subfolders_name)):
    y=exp['Location']==subfolders_name[i]
    indices_true=[index for index, item in enumerate(y)
            if item is True]
    print(exp[indices_true, 'Field 1'])

I am not sure whether this is the right approach, or there is just something missing from my code. Thank you!

You can do this using the following:

df = exp[exp['Field 1'].isin(subfolders_name)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM