对于pandas数据帧中的每一行,我想找到具有最小值的单元格/单元格,并分别返回其行和列名称。我还想检查最小值是否小于1。
,例如,
NAMES, Oil, Fat, Salt
Salad, 0.2, 0.1, 0.8
Bread, 0.1, 0.9, 0.1
Rice, 1, 1, 1
以上数据框的输出:
['Salad', 'Fat']
[['Bread', 'Oil'], ['Bread', 'Salt']]
<No output because the minimum is not less than 1>
我目前的代码如下所示,
pairs = df.set_index('NAMES').apply(lambda row: [[row.name, l] for l in row[row == row.min()].index], axis=1).values.tolist()
请帮帮我。
答案 0 :(得分:3)
使用
public static Value? Get<Key, Value>(this Dictionary<Key, Value> from, Key by)
where Value : struct
{
Value result;
return from.TryGetValue(by, out result) ? result : (Value?)null;
}
public static Value Get<Key, Value>(this Dictionary<Key, Value> from, Key by)
where Value : class
{
Value result;
return from.TryGetValue(by, out result) ? result : null;
}
如果您需要删除空列表,请使用过滤器
In [2224]: dff = df.set_index('NAMES')
In [2225]: dff.where(dff<1).eq(dff.min(1), axis=0).apply(
lambda x: [[x.name, v] for v in x[x].index], axis=1).values.tolist()
Out[2225]: [[['Salad', ' Fat']], [['Bread', ' Oil'], ['Bread', ' Salt']], []]
或者,
In [2230]: vals = dff.where(dff<1).eq(dff.min(1), axis=0).apply(
lambda x: [[x.name, v] for v in x[x].index], axis=1).values.tolist()
In [2231]: filter(None, vals)
Out[2231]: [[['Salad', ' Fat']], [['Bread', ' Oil'], ['Bread', ' Salt']]]
答案 1 :(得分:0)
我认为你需要添加新条件:
pairs = (df.set_index('NAMES')
.apply(lambda row: [[row.name, l] for l in row[(row == row.min()) &
(row < 1)].index], axis=1)
.values.tolist())
print (pairs)
[[['Salad', 'Fat']], [['Bread', 'Oil'], ['Bread', 'Salt']], []]
然后使用this solutions:
pairs = [x for x in pairs if x]
print (pairs)
[[['Salad', 'Fat']], [['Bread', 'Oil'], ['Bread', 'Salt']]]
#for python 3 add list
pairs = list(filter(None, pairs))
print (pairs)
[[['Salad', 'Fat']], [['Bread', 'Oil'], ['Bread', 'Salt']]]
pairs = [x for x in pairs if x != []]
print (pairs)
[[['Salad', 'Fat']], [['Bread', 'Oil'], ['Bread', 'Salt']]]