将不同长度的groupby数据组合到Dataframe中,以及如何解决IndexError:列表索引超出范围



我想将逐组数据重组为数据帧。因为表中有不同长度的groupbyed数据,所以当我迭代这些groupbyed的数据时,会出现IndexError:list index out of range,我不知道如何忽略这个索引错误。

我的数据背景。

  • 我分组为";Worker_id";以及";Project_id">
Worker_IDYYYYYYYYQ[/tr>Q[/tr>Q[/tr>QQ
问题 答案项目ID
DD AB X Y
DD AB X
DD AB X
BD BG K
BD BG K
KY GG J
KY GG J
KY GG J
KY GG J
RR FR X Q
HU RT K
HU RT K
HU RT K
YU GE J
YU GE J
XX FF K P
XX FF K P
XI UF J P
XI UF J P

以下是我迄今为止所拥有的。正如我在评论中所说,行数比您想要的输出中的行数多,但我不知道在什么条件下应该加入它们。

# restructure the df with `pivot_table`
m1 = df.groupby('Project_ID').cumcount() + 1
m2 = pd.factorize(df['Worker_ID'])[0] + 1
out = (
df
.pivot_table(
index=['Project_ID', m1], 
columns=m2, 
values=['Worker_ID', 'Question', 'Answer'], 
aggfunc='first'
)
)

# join the multiindex columns to single columns
out.columns = out.columns.map(lambda x: f"{x[0]}_{x[1]}")
# in case you need to automate the sorting of the columns, here is how to do it (otherwise you could just pass a list with the new order of the columns) 
# 1st sort by numbers, then always bring them in the right order (question, answer, worker)
pattern = '|'.join([f"({x})" for x in ['Question', 'Answer', 'Worker']])
def sort_key(x):
return re.search(pattern,x).lastindex
new_order = sorted(out.columns.tolist(), key=lambda x: (int(x[-1]), sort_key(x.split('_')[0])))
out = out[new_order]
print(out)
Question_1 Answer_1 Worker_ID_1 Question_2 Answer_2 Worker_ID_2 Question_3 Answer_3 Worker_ID_3
Project_ID                                                                                                  
P          1        NaN      NaN         NaN         XX       FF           K        NaN      NaN         NaN
2        NaN      NaN         NaN         XX       FF           K        NaN      NaN         NaN
3        NaN      NaN         NaN        NaN      NaN         NaN         XI       UF           J
4        NaN      NaN         NaN        NaN      NaN         NaN         XI       UF           J
Q          1         RR       FR           X        NaN      NaN         NaN        NaN      NaN         NaN
2        NaN      NaN         NaN         HU       RT           K        NaN      NaN         NaN
3        NaN      NaN         NaN         HU       RT           K        NaN      NaN         NaN
4        NaN      NaN         NaN         HU       RT           K        NaN      NaN         NaN
5        NaN      NaN         NaN        NaN      NaN         NaN         YU       GE           J
6        NaN      NaN         NaN        NaN      NaN         NaN         YU       GE           J
Y          1         DD       AB           X        NaN      NaN         NaN        NaN      NaN         NaN
2         DD       AB           X        NaN      NaN         NaN        NaN      NaN         NaN
3         DD       AB           X        NaN      NaN         NaN        NaN      NaN         NaN
4        NaN      NaN         NaN         BD       BG           K        NaN      NaN         NaN
5        NaN      NaN         NaN         BD       BG           K        NaN      NaN         NaN
6        NaN      NaN         NaN        NaN      NaN         NaN         KY       GG           J
7        NaN      NaN         NaN        NaN      NaN         NaN         KY       GG           J
8        NaN      NaN         NaN        NaN      NaN         NaN         KY       GG           J
9        NaN      NaN         NaN        NaN      NaN         NaN         KY       GG           J

最新更新