试图通过匹配行值来选择一列,但却返回该列中另一行的值。

问题描述 投票:0回答:1

我有一个熊猫数据框架

       field          sev         iso         des 
0  shortname          sev         iso         des 
1   fullname  Sevoflurane  Isoflurane  Desflurane
2        id             0           1           2
3 colorname          Gold Dark Magenta Royal Blue
4  colorHex       #FFD700     #8B008B     #4169E1
5       mac           2.1        1.15         5.8

我试图找到正确的pandas语法来搜索 "id "行与值1匹配的列,并返回相同列的 "mac "值。

如果可以选择带有

c = df.loc['id'] = 1

现在我试图获取列 "mac "的值,但没有成功。

_mac = csv_rx_df.at[c, 'mac']

我怎么做呢?

python pandas dataframe jupyter lookup
1个回答
0
投票
#set 'field' as index
df = df.set_index('field')
#get the column where id row equals "1", and subsequently,
#get the value for 'mac'
df.loc['mac',df.loc['id',:].eq("1")]

iso    1.15
Name: mac, dtype: object
© www.soinside.com 2019 - 2024. All rights reserved.