我有一个Dataframe。我将它按a列、b列进行分组,并对每个组应用一个函数。函数返回一个字典列表,其中每个字典都必须作为行附加到新的Dataframe中。我就是这么做的:
def foo(x):
new_row = []
new_row_dict = {}
for i in items:
new_row_dict['A'] = value1
new_row_dict['B'] = value2
new_row_dict['C'] = value3
new_row_dict['D'] = value4
new_row.append(new_row_dict)
return new_row # Returns a list of dictionaries
# new_row = [{'A': value1, 'B': value2, 'C':value3, 'D':value4}, {'A': value11, 'B': value22, 'C':value33, 'D':value44}]
df = pd.DataFrame(columns = ["A", "B", "C", "D"])
df = df.append([foo(x) for _, x in df1.groupby(['A', 'B'])], ignore_index=True)
但我得到了一个错误: AttributeError: 'list' object has no attribute 'keys'
. 有没有有效的方法?
暂无答案!
目前还没有任何答案,快来回答吧!