根据另一行中的值选择DataFrame中的一行的值

时间:2019-03-13 00:02:02

标签: python pandas

我有以下示例DataFrame

<form>

在此示例中,我想在“位置” =“ nyc”的“值”行中检索数字,因此在这种情况下,我想检索数字5。我正在尝试使用.loc布尔值,但不能完全正确

2 个答案:

答案 0 :(得分:1)

您可能需要两个def as_two_col_layout(self): return self._html_output( normal_row='<div class="form-group m-form__group row">' '<label class="col-sm-3 col-form-label">%(label)s</label>' '<div class="col-sm-9">%(field)s%(help_text)s</div></div>', error_row='%s', row_ender='</div></div>', help_text_html=' <span class="m-form__help">%s</span>', errors_on_separate_row=True)

.loc

如果只需要输出值

df2.loc['value',df2.loc['location']=='nyc']
Out[269]: 
col1    5
Name: value, dtype: object

答案 1 :(得分:0)

这将为您提供value行中的值,但是我正在使用iloc而不是loc对其进行索引:

df2.iloc[0, np.where(df2.loc['location', ] == 'nyc')[0]].values

输出:

array(['5'], dtype=object)