我有一组具有相同键的字典,并且我已经有一个带有 df header 的 pandas 模板。我需要循环遍历数组并将每个字典附加到 df。
arr_dics = [{'event':'alpha', 'site':'beta', 'date':'gamma'}, {'event':'a', 'site':'b', 'date':'c'}]
for i in range(len(arr_dics)):
new_dic = arr_dics[i]
two_games_df.append(new_dic, ignore_index=True)
但是我得到的输出仅将第二个字典附加到数据帧。 我需要在某处指定索引吗?
最佳答案
为了避免您的困惑,请注意 append
不是 inplace
。因此,您需要将 append
函数的输出分配给原始 df
:
two_games_df= two_games_df.append(new_dic, ignore_index=True)
关于python - 将列表中的字典附加到 pandas 数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62191622/