选择的列和行的pandas groupby和agg操作

时间:2019-10-22 05:31:00

标签: pandas pandas-groupby

我有一个如下数据框:

enter image description here

我不确定是否可以使用熊猫进行如下输出:

difference = Response [df.Time ==“ pre”]-每个组的Response.min

enter image description here

1 个答案:

答案 0 :(得分:1)

如果public static class Pair<F, S> { private F first; private S second; public Pair(F first, S second) { this.first = first; this.second = second; } } public static void main(String[] args) { // criteria is a function Function<String, Pair<Boolean, String>> criteria = (x) -> new Pair<>(true, x); Stream.of(criteria) .map(func -> func.apply(null)) .filter(result -> !result.first) .map(result -> result.second) .collect(Collectors.joining(",")); // criteria is a list List<Function<String, Pair<Boolean, String>>> criterias = new ArrayList<>(); criterias.stream() .map(func -> func.apply(null)) .filter(result -> !result.first) .map(result -> result.second) .collect(Collectors.joining(",")); } 始终是每组的第一名,则应重复输出中的值:

pre

仅可能将每个组的第一个值替换为空字符串,但会得到混合值-带字符串的数字,因此下一个处理应该是问题:

df['diff'] = df.groupby('IDs')['Response'].transform(lambda x: (x.iat[0] - x).min())

编辑:

df['diff'] = df['diff'].mask(df['diff'].duplicated(), '')