python - 将列表中的字典附加到 pandas 数据框

标签 python pandas

我有一组具有相同键的字典,并且我已经有一个带有 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/

相关文章:

python - 根据条件替换所有列中的所有值

python - pandas 中的统计分析和绘图(python)

python - Pandas 重采样 : TypeError: Only valid with DatetimeIndex, TimedeltaIndex 或 PeriodIndex,但得到了 'RangeIndex' 的实例

python - 根据两个表的一列中共有的值对两个表进行求和

python - pandas数据框浮点问题

python - "Play"未定义 - Pylance (reportUndefinedVariable)

python - gdb python api : is it possible to make a call to a class/struct method

python - Pycharm 中是否有用于重命名特定变量的键盘快捷键?

python - 如果 AppEngine 应用程序使用 GCS,那么该应用程序的用户如何上传大文件?

python - (BigQuery PY Client Library v0.28) - 从表 'query' 作业中获取结果