簡體   English   中英

如果另一個單元格與列表中的字符串匹配,如何從 excel 單元格獲取值

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

我有下表:

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

以及以下列表:

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

我想要做的是當我的表中的位置列與列表中的字符串匹配時,從細胞類型、處理、字段 1 和字段 2 列中提取信息。

例如,我希望獲得的是:如果 [在位置列中] 表中的 r01c01 出現在我的字符串列表中,則為每個單獨的信息獲取一個變量,如下所示:

cell_type=HCT WT

treatment=NT

field_1=25

field_2=34

我嘗試了以下代碼只是為了從字段 1 獲取信息,但是我收到以下錯誤: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'])

我不確定這是否是正確的方法,或者我的代碼中缺少一些東西。 謝謝!

您可以使用以下方法執行此操作:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM