Python 如何使用NAN解包一行数据帧内列表中的多个字典对象?
我有一个dataframe,每行和每行的单个列表中包含以下词典,列表大小不同,它们的大小不同,如下所示:Python 如何使用NAN解包一行数据帧内列表中的多个字典对象?,python,python-3.x,pandas,Python,Python 3.x,Pandas,我有一个dataframe,每行和每行的单个列表中包含以下词典,列表大小不同,它们的大小不同,如下所示: ID unnest_column 1 [{'abc': 11, 'def': 1},{'abc': 15, 'def': 1}, {'abc': 16, 'def': 1}, {'abc': 17, 'def': 1}, {'abc': 18, 'def': 1, 'ghi': 'abc'}, {'abc': 23, 'def': 'x
ID unnest_column
1 [{'abc': 11, 'def': 1},{'abc': 15, 'def': 1},
{'abc': 16, 'def': 1},
{'abc': 17, 'def': 1},
{'abc': 18, 'def': 1, 'ghi': 'abc'},
{'abc': 23, 'def': 'xxx', 'def': 1},
{'abc': 23, 'def': 'xxx', 'def': 2},
{'abc': 23, 'def': 'xxx', 'def': 4}]
2 NaN
3 [{'abc': 11, 'def': 1}]
如何解压列表中的词典并生成键值列
新的df可能不确定其外观,只需要在列中输入键:
id abc def ghi
1 2 3 abc
我从我问的另一个问题中得到了这个代码:
inds = [index for _ in ([i] * len(v) for i,v in df.x.iteritems()) for index in _]
pd.DataFrame(df.x.values.sum(), index=inds)
但问题是,NAN导致第二条线路故障,我得到一个错误:
can only concatenate list (not "str") to list
如果将相关列的数据类型更改为string,则第二行代码会出现以下错误:
ValueError: DataFrame constructor not properly called!
如果我只运行第二行的内部,在括号内,我得到错误:
SyntaxError: can't assign to function call
我不知道你想要什么样的产品,除非我明白,否则我帮不了你that@RushabhMehta这是最初的问题。我必须打开另一列,但其中有nans,不允许我打开。是的,显然它不允许您解包,您希望程序对NAN做什么?同样,您需要为这个示例发布所需的输出