我正在分析来自Facebook的政治广告,这是由ProPublica发布的
这就是我的意思。 我有一整列的目标,我想分析,但它的格式在一个非常难以接近的方式为我的技能水平的人
这仅来自1个单元:
[{"target": "NAge", "segment": "21 and older"}, {"target": "MinAge", "segment": "21"}, {"target": "Retargeting", "segment": "people who may be similar to their customers"}, {"target": "Region", "segment": "the United States"}]
还有一个:
[{"target": "NAge", "segment": "18 and older"}, {"target": "Location Type", "segment": "HOME"}, {"target": "Interest", "segment": "Hispanic culture"}, {"target": "Interest", "segment": "Republican Party (United States)"}, {"target": "Location Granularity", "segment": "country"}, {"target": "Country", "segment": "the United States"}, {"target": "MinAge", "segment": 18}]
我需要做的是分离每个“目标”项,使其成为列标签,并使每个对应的“段”成为该列中的一个可能值
或者,解决方案是创建一个函数来调用每行中的每个字典键来计算频率
lists
的dicts
。pandas.explode()
将list
中的每个dict
移动到单独的列中李>pandas.json_normalize()
,.join()
将dicts
的列转换为数据帧,其中键是列标题,值是观察值,然后使用pandas.json_normalize()
,.join()
返回到df
李>.drop()
删除不需要的列李>"[{key: value}]"
),请参考Splitting dictionary/list inside a Pandas Column into Separate Columns中的solution,并使用:df.col2 = df.col2.apply(literal_eval)
,带from ast import literal_eval
李>display(df)
获取
count
'target'
和关联的'segment'
获取count
更新
相关问题 更多 >
编程相关推荐