将字典列表转换为数据框时处理缺失值

Dealing with missing values when converting a list of dictionaries into a dataframe

在将 dictionarieslist 转换为 pandas dataframe 时,有没有办法处理缺失值?有时 dictionary 条目的顺序不同,所以我必须分别处理每一列。

这是一个例子:

p = [
 {'c': 53.13,'n': 1,'t': 1575050400000},
 {'t': 1575048600000,'c': 53.11}
  ]

这是我一直在尝试的:

import pandas as pd
df = pd.DataFrame([{
        "c": t["c"],
        "n": t["n"],
        "t": t['t']}
        for t in p])

我得到一个 KeyError: 'n',因为 'n' 的条目在第二个 dictionary 中丢失。有没有一种方法可以在条目丢失时只放一个 NaN

您可以从 p 实例化一个 DataFrame 作为参数:

df = pd.DataFrame(p)
df

输出:

       c    n              t
0  53.13  1.0  1575050400000
1  53.11  NaN  1575048600000