【问题标题】:Trying to select a column by matching a row value but return the value of another row in that column尝试通过匹配行值来选择列,但返回该列中另一行的值
【发布时间】:2020-05-04 10:31:53
【问题描述】:

我有一个熊猫数据框

       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']

我该怎么做?

【问题讨论】:

  • 看起来你的 DF 真的应该被转置 - 那是猜测你想要 2.1 for id = 1?
  • df.iloc[5][df.iloc[2] == 1]。试试这个切片条件,看看它是否有效。
  • 异氟醚'mac' = 1.15

标签: python pandas dataframe jupyter lookup


【解决方案1】:
#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

【讨论】:

  • df.set_index('field', inplace=True) result = df.loc['mac', df.loc['id' ,:].eq("1")] 工作正常,现在我做一个def()函数,再次感谢大家的帮助
  • 如果它对你有用,别忘了投票并接受作为答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-28
  • 1970-01-01
  • 1970-01-01
  • 2020-03-12
相关资源
最近更新 更多