gpt4 book ai didi

python - 对 df 中的唯一值执行 groupby 计数的有效方法

转载 作者:行者123 更新时间:2023-12-04 10:43:15 25 4
gpt4 key购买 nike

下面的代码旨在返回 df计算来自引用点的正负点数 (mainX, mainY) .这是由 Direction 决定的.这些被分为两组 (I, J) .这些点位于X,Y每个人都有一个亲戚 Label .

所以我把这些点分成各自的组。然后我将 df 子集使用查询进入正/负 df。这些 df 然后按时间分组并计入单独的列。然后将这些 df 连接起来。

所有这些似乎都非常低效。特别是如果我在 Group 中有许多唯一值.例如,我必须复制查询序列以返回 Group J 的计数。 .

有没有更有效的方法来完成预期的输出?

import pandas as pd

df = pd.DataFrame({
'Time' : ['09:00:00.1','09:00:00.1','09:00:00.1','09:00:00.1','09:00:00.1','09:00:00.1','09:00:00.2','09:00:00.2','09:00:00.2','09:00:00.2','09:00:00.2','09:00:00.2'],
'Group' : ['I','J','I','J','I','J','I','J','I','J','I','J'],
'Label' : ['A','B','C','D','E','F','A','B','C','D','E','F'],
'X' : [8,4,3,8,7,4,2,3,3,4,6,1],
'Y' : [3,6,4,8,5,2,8,8,2,4,5,1],
'mainX' : [5,5,5,5,5,5,5,5,5,5,5,5],
'mainY' : [5,5,5,5,5,5,5,5,5,5,5,5],
'Direction' : ['Left','Right','Left','Right','Left','Right','Left','Right','Left','Right','Left','Right']
})

# Determine amount of unique groups
Groups = df['Group'].unique().tolist()

# Subset groups into separate df's
Group_I = df.loc[df['Group'] == Groups[0]]
Group_J = df.loc[df['Group'] == Groups[1]]


# Separate into positive and negative direction for each group
GroupI_Pos = Group_I.query("(Direction == 'Right' and X > mainX) or (Direction == 'Left' and X < mainX)").copy()
GroupI_Neg = Group_I.query("(Direction == 'Right' and X < mainX) or (Direction == 'Left' and X > mainX)").copy()

# Count of items per timestamp for Group I
GroupI_Pos['GroupI_Positive_Count'] = GroupI_Pos.groupby(['Time'])['Time'].transform('count')
GroupI_Neg['GroupI_Negative_Count'] = GroupI_Neg.groupby(['Time'])['Time'].transform('count')

# Combine Positive/Negative dfs
df_I = pd.concat([GroupI_Pos, GroupI_Neg], sort = False).sort_values(by = 'Time')

# Forward fill Nan grouped by time
df_I = df_I.groupby(['Time']).ffill()

预期输出:
          Time Group Label  X  Y  mainX  mainY Direction  GroupI_Positive_Count  GroupI_Negative_Count  GroupJ_Positive_Count  GroupJ_Negative_Count
0 09:00:00.1 I A 8 3 5 5 Left 1 2 1 2
1 09:00:00.1 J B 4 6 5 5 Right 1 2 1 2
2 09:00:00.1 I C 3 4 5 5 Left 1 2 1 2
3 09:00:00.1 J D 8 8 5 5 Right 1 2 1 2
4 09:00:00.1 I E 7 5 5 5 Left 1 2 1 2
5 09:00:00.1 J F 4 2 5 5 Right 1 2 1 2
6 09:00:00.2 I A 2 8 5 5 Left 2 1 0 3
7 09:00:00.2 J B 3 8 5 5 Right 2 1 0 3
8 09:00:00.2 I C 3 2 5 5 Left 2 1 0 3
9 09:00:00.2 J D 4 4 5 5 Right 2 1 0 3
10 09:00:00.2 I E 6 5 5 5 Left 2 1 0 3
11 09:00:00.2 J F 1 1 5 5 Right 2 1 0 3

最佳答案

这是我的看法

s = (((df.Direction.eq('Right') & df.X.gt(df.mainX)) | 
(df.Direction.eq('Left') & df.X.lt(df.mainX)))
.replace({True: 'Pos', False: 'Neg'}))

df_count = df.groupby(['Time', 'Group', s]).size().unstack([1, 2], fill_value=0)
df_count.columns = df_count.columns.map(lambda x: f'Group{x[0]}_{x[1]}')

df_final = df.merge(df_count, left_on='Time', right_index=True)

Out[521]:
Time Group Label X Y mainX mainY Direction GroupI_Neg \
0 09:00:00.1 I A 8 3 5 5 Left 2
1 09:00:00.1 J B 4 6 5 5 Right 2
2 09:00:00.1 I C 3 4 5 5 Left 2
3 09:00:00.1 J D 8 8 5 5 Right 2
4 09:00:00.1 I E 7 5 5 5 Left 2
5 09:00:00.1 J F 4 2 5 5 Right 2
6 09:00:00.2 I A 2 8 5 5 Left 1
7 09:00:00.2 J B 3 8 5 5 Right 1
8 09:00:00.2 I C 3 2 5 5 Left 1
9 09:00:00.2 J D 4 4 5 5 Right 1
10 09:00:00.2 I E 6 5 5 5 Left 1
11 09:00:00.2 J F 1 1 5 5 Right 1

GroupI_Pos GroupJ_Neg GroupJ_Pos
0 1 2 1
1 1 2 1
2 1 2 1
3 1 2 1
4 1 2 1
5 1 2 1
6 2 3 0
7 2 3 0
8 2 3 0
9 2 3 0
10 2 3 0
11 2 3 0

关于python - 对 df 中的唯一值执行 groupby 计数的有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59832860/

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