gpt4 book ai didi

python - Pandas DataFrame 窗口函数

转载 作者:行者123 更新时间:2023-11-28 18:33:28 26 4
gpt4 key购买 nike

我正在尝试以类似于您使用 SQL 窗口函数的方式来操作我的数据框。考虑以下样本集:

import pandas as pd

df = pd.DataFrame({'fruit' : ['apple', 'apple', 'apple', 'orange', 'orange', 'orange', 'grape', 'grape', 'grape'],
'test' : [1, 2, 1, 1, 2, 1, 1, 2, 1],
'analysis' : ['full', 'full', 'partial', 'full', 'full', 'partial', 'full', 'full', 'partial'],
'first_pass' : [12.1, 7.1, 14.3, 19.1, 17.1, 23.4, 23.1, 17.2, 19.1],
'second_pass' : [20.1, 12.0, 13.1, 20.1, 18.5, 22.7, 14.1, 17.1, 19.4],
'units' : ['g', 'g', 'g', 'g', 'g', 'g', 'g', 'g', 'g'],
'order' : [2, 1, 3, 2, 1, 3, 3, 2, 1]})
+--------+------+----------+------------+-------------+-------+-------+| fruit  | test | analysis | first_pass | second_pass | order | units |+--------+------+----------+------------+-------------+-------+-------+| apple  |    1 | full     | 12.1       | 20.1        |     2 | g     || apple  |    2 | full     | 7.1        | 12.0        |     1 | g     || apple  |    1 | partial  | 14.3       | 13.1        |     3 | g     || orange |    1 | full     | 19.1       | 20.1        |     2 | g     || orange |    2 | full     | 17.1       | 18.5        |     1 | g     || orange |    1 | partial  | 23.4       | 22.7        |     3 | g     || grape  |    1 | full     | 23.1       | 14.1        |     3 | g     || grape  |    2 | full     | 17.2       | 17.1        |     2 | g     || grape  |    1 | partial  | 19.1       | 19.4        |     1 | g     |+--------+------+----------+------------+-------------+-------+-------+

我想添加几列:

  • 一个 bool 列,用于指示该测试和分析的 second_pass 值是否是所有水果类型中最高的。
  • 另一列列出了每种测试和分析组合中哪些水果具有最高的 second_pass 值。

使用这个逻辑,我想得到下表:

+--------+------+----------+------------+-------------+-------+-------+---------+---------------------+| fruit  | test | analysis | first_pass | second_pass | order | units | highest |   highest_fruits    |+--------+------+----------+------------+-------------+-------+-------+---------+---------------------+| apple  |    1 | full     | 12.1       | 20.1        |     2 | g     | true    | ["apple", "orange"] || apple  |    2 | full     | 7.1        | 12.0        |     1 | g     | false   | ["orange"]          || apple  |    1 | partial  | 14.3       | 13.1        |     3 | g     | false   | ["orange"]          || orange |    1 | full     | 19.1       | 20.1        |     2 | g     | true    | ["apple", "orange"] || orange |    2 | full     | 17.1       | 18.5        |     1 | g     | true    | ["orange"]          || orange |    1 | partial  | 23.4       | 22.7        |     3 | g     | true    | ["orange"]          || grape  |    1 | full     | 23.1       | 22.1        |     3 | g     | false   | ["orange"]          || grape  |    2 | full     | 17.2       | 17.1        |     2 | g     | false   | ["orange"]          || grape  |    1 | partial  | 19.1       | 19.4        |     1 | g     | false   | ["orange"]          |+--------+------+----------+------------+-------------+-------+-------+---------+---------------------+

我是 pandas 的新手,所以我确定我错过了一些非常简单的东西。

最佳答案

您可以返回 boolean 值,其中 second_pass 等于 group max,如 idxmax 只返回第一次出现的 max:

df['highest'] = df.groupby(['test', 'analysis'])['second_pass'].transform(lambda x: x == np.amax(x)).astype(bool)

然后使用 np.where 来捕获所有具有 group maxfruit 值,并且 merge 结果到你的 DataFrame 中,像这样:

highest_fruits = df.groupby(['test', 'analysis']).apply(lambda x: [f for f in np.where(x.second_pass == np.amax(x.second_pass), x.fruit.tolist(), '').tolist() if f!='']).reset_index()
df =df.merge(highest_fruits, on=['test', 'analysis'], how='left').rename(columns={0: 'highest_fruit'})

最后,为了您的跟进:

first_pass = df.groupby(['test', 'analysis']).apply(lambda x: {fruit: x.loc[x.fruit==fruit, 'first_pass'] for fruit in x.highest_fruit.iloc[0]}).reset_index()
df =df.merge(first_pass, on=['test', 'analysis'], how='left').rename(columns={0: 'first_pass_highest_fruit'})

得到:

  analysis  first_pass   fruit  order  second_pass  test units highest  \
0 full 12.1 apple 2 20.1 1 g True
1 full 7.1 apple 1 12.0 2 g False
2 partial 14.3 apple 3 13.1 1 g False
3 full 19.1 orange 2 20.1 1 g True
4 full 17.1 orange 1 18.5 2 g True
5 partial 23.4 orange 3 22.7 1 g True
6 full 23.1 grape 3 14.1 1 g False
7 full 17.2 grape 2 17.1 2 g False
8 partial 19.1 grape 1 19.4 1 g False

highest_fruit first_pass_highest_fruit
0 [apple, orange] {'orange': [19.1], 'apple': [12.1]}
1 [orange] {'orange': [17.1]}
2 [orange] {'orange': [23.4]}
3 [apple, orange] {'orange': [19.1], 'apple': [12.1]}
4 [orange] {'orange': [17.1]}
5 [orange] {'orange': [23.4]}
6 [apple, orange] {'orange': [19.1], 'apple': [12.1]}
7 [orange] {'orange': [17.1]}
8 [orange] {'orange': [23.4]}

关于python - Pandas DataFrame 窗口函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34735915/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com