gpt4 book ai didi

python - DataFrame 在列表中重复字典

转载 作者:行者123 更新时间:2023-11-28 22:13:11 25 4
gpt4 key购买 nike

我有一个包含嵌套字典列表的 JSON 文件 - (Json 示例):

{"posts": [{"url": "http://twitter.com/AkEl_Saruman/status/1084067040481169408", "title": "", "type": "Twitter", "language": "tr", "assignedCategoryId": 19058723389, "assignedEmotionId": 0, "categoryScores": [{"categoryId": 19058723389, "categoryName": "Basic Negative", "score": 0.58}, {"categoryId": 19058723388, "categoryName": "Basic Positive", "score": 0.01}, {"categoryId": 19058723391, "categoryName": "Basic Neutral", "score": 0.41}], "emotionScores": [], "imageInfo": [], "monitorId": 19058723386, "guid": "1084067040481169408", "parentGuid": "1083973777493512192", "engagementType": "RETWEET", "documentsUrls": ["https://www.yenisafak.com/dunya/firatin-dogusunda-turkiyeye-sabotaj-3430594"]}, {"url": "http://twitter.com/eazngl2/status/1084067263895007232", "title": "", "type": "Twitter", "location": "SAU", "geolocation": {"id": "SAU", "name": "Saudi Arabia", "country": "SAU"}, "language": "ar", "assignedCategoryId": 19058723391, "assignedEmotionId": 0, "categoryScores": [{"categoryId": 19058723389, "categoryName": "Basic Negative", "score": 0.01}, {"categoryId": 19058723388, "categoryName": "Basic Positive", "score": 0.39}, {"categoryId": 19058723391, "categoryName": "Basic Neutral", "score": 0.6}], "emotionScores": [], "imageInfo": [], "monitorId": 19058723386, "guid": "1084067263895007232", "parentGuid": "1084044740461502465", "engagementType": "RETWEET", "mediaUrls": ["http://pbs.twimg.com/media/DwtM8PYW0AEE-oR.jpg"]}, {"url": "http://twitter.com/h_7hm/status/1084067289723535360", "title": "", "type": "Twitter", "location": "SAU", "geolocation": {"id": "SAU", "name": "Saudi Arabia", "country": "SAU"}, "language": "ar", "assignedCategoryId": 19058723391, "assignedEmotionId": 0, "categoryScores": [{"categoryId": 19058723389, "categoryName": "Basic Negative", "score": 0.17}, {"categoryId": 19058723388, "categoryName": "Basic Positive", "score": 0.01}, {"categoryId": 19058723391, "categoryName": "Basic Neutral", "score": 0.81}], "emotionScores": [], "imageInfo": [], "monitorId": 19058723386, "guid": "1084067289723535360", "parentGuid": "1083854364547207175", "engagementType": "RETWEET"}, {"url": "http://twitter.com/BeooutQ_2BQ/status/1084067316311224325", "title": "", "type": "Twitter", "location": "Bogota, Bogota, COL", "geolocation": {"id": "COL.Bogota.Bogota", "name": "Bogota", "country": "COL", "state": "Bogota", "city": "Bogota"}, "language": "ar", "assignedCategoryId": 19058723389, "assignedEmotionId": 0, "categoryScores": [{"categoryId": 19058723389, "categoryName": "Basic Negative", "score": 0.52}, {"categoryId": 19058723388, "categoryName": "Basic Positive", "score": 0.24}, {"categoryId": 19058723391, "categoryName": "Basic Neutral", "score": 0.24}], "emotionScores": [], "imageInfo": [], "monitorId": 19058723386, "guid": "1084067316311224325", "parentGuid": "1084066998399758336", "engagementType": "REPLY"}, {"url": "http://twitter.com/ekspreshbrajans/status/1084067335680471040", "title": "", "type": "Twitter", "location": "Adana, Mediterranean Region, TUR", "geolocation": {"id": "TUR.Mediterranean Region.Adana", "name": "Adana", "country": "TUR", "state": "Mediterranean Region", "city": "Adana"}, "language": "tr", "authorGender": "M", "assignedCategoryId": 19058723389, "assignedEmotionId": 0, "categoryScores": [{"categoryId": 19058723389, "categoryName": "Basic Negative", "score": 0.57}, {"categoryId": 19058723388, "categoryName": "Basic Positive", "score": 0.04}, {"categoryId": 19058723391, "categoryName": "Basic Neutral", "score": 0.39}], "emotionScores": [], "imageInfo": [], "monitorId": 19058723386, "guid": "1084067335680471040", "documentsUrls": ["http://ekspreshaberajansi.com/2019/01/12/fuat-ugur-feto-neyse-sozcu-de-o/"]}, {"url": "http://twitter.com/mualitass/status/1084067769094754305", "title": "", "type": "Twitter", "location": "Istanbul, Marmara Region, TUR", "geolocation": {"id": "TUR.Marmara Region.Istanbul", "name": "Istanbul", "country": "TUR", "state": "Marmara Region", "city": "Istanbul"}, "language": "tr", "authorGender": "M", "assignedCategoryId": 19058723389, "assignedEmotionId": 0, "categoryScores": [{"categoryId": 19058723389, "categoryName": "Basic Negative", "score": 0.96}, {"categoryId": 19058723388, "categoryName": "Basic Positive", "score": 0.03}, {"categoryId": 19058723391, "categoryName": "Basic Neutral", "score": 0.01}], "emotionScores": [], "imageInfo": [], "monitorId": 19058723386, "guid": "1084067769094754305", "parentGuid": "1084020709586845696", "engagementType": "RETWEET"}, {"url": "http://twitter.com/smail_gomra/status/1084067900732907520", "title": "", "type": "Twitter", "language": "ar", "assignedCategoryId": 19058723391, "assignedEmotionId": 0, "categoryScores": [{"categoryId": 19058723389, "categoryName": "Basic Negative", "score": 0.0}, {"categoryId": 19058723388, "categoryName": "Basic Positive", "score": 0.32}, {"categoryId": 19058723391, "categoryName": "Basic Neutral", "score": 0.68}], "emotionScores": [], "imageInfo": [], "monitorId": 19058723386, "guid": "1084067900732907520", "parentGuid": "1084062244781113347", "engagementType": "RETWEET", "mediaUrls": ["https://video.twimg.com/ext_tw_video/1084060799595933698/pu/vid/640x480/bZ1hcR-mCViaG2vQ.mp4?tag=8"]}, {"url": "http://twitter.com/taliphan_197878/status/1084067941556068352", "title": "", "type": "Twitter", "location": "Izmir, Aegean Region, TUR", "geolocation": {"id": "TUR.Aegean Region.Izmir", "name": "Izmir", "country": "TUR", "state": "Aegean Region", "city": "Izmir"}, "language": "tr", "assignedCategoryId": 19058723391, "assignedEmotionId": 0, "categoryScores": [{"categoryId": 19058723389, "categoryName": "Basic Negative", "score": 0.24}, {"categoryId": 19058723388, "categoryName": "Basic Positive", "score": 0.05}, {"categoryId": 19058723391, "categoryName": "Basic Neutral", "score": 0.72}], "emotionScores": [], "imageInfo": [], "monitorId": 19058723386, "guid": "1084067941556068352", "parentGuid": "1084016623294525440", "engagementType": "RETWEET", "documentsUrls": ["https://m.turkiyegazetesi.com.tr/yazarlar/fuat-ugur/606039.aspx"]}, {"url": "http://twitter.com/spor26/status/1084067995326963714", "title": "", "type": "Twitter", "location": "Eskisehir, Central Anatolian Region, TUR", "geolocation": {"id": "TUR.Central Anatolian Region.Eskisehir", "name": "Eskisehir", "country": "TUR", "state": "Central Anatolian Region", "city": "Eskisehir"}, "language": "tr", "assignedCategoryId": 19058723391, "assignedEmotionId": 0, "categoryScores": [{"categoryId": 19058723389, "categoryName": "Basic Negative", "score": 0.11}, {"categoryId": 19058723388, "categoryName": "Basic Positive", "score": 0.04}, {"categoryId": 19058723391, "categoryName": "Basic Neutral", "score": 0.85}], "emotionScores": [], "imageInfo": [], "monitorId": 19058723386, "guid": "1084067995326963714", "documentsUrls": ["http://dlvr.it", "http://www.spor26.com/haberdetay/10980-mehmet-ozcana-fransadan-talip.html?utm_source=dlvr.it&utm_medium=twitter"], "mediaUrls": ["http://pbs.twimg.com/media/DwtiGMAUwAIlqAH.jpg"]}, {"url": "http://twitter.com/xcV44s101gjyOn3/status/1084067920781733888", "title": "", "type": "Twitter", "location": "DZA", "geolocation": {"id": "DZA", "name": "Algeria", "country": "DZA"}, "language": "ar", "assignedCategoryId": 19058723391, "assignedEmotionId": 0, "categoryScores": [{"categoryId": 19058723389, "categoryName": "Basic Negative", "score": 0.0}, {"categoryId": 19058723388, "categoryName": "Basic Positive", "score": 0.32}, {"categoryId": 19058723391, "categoryName": "Basic Neutral", "score": 0.68}], "emotionScores": [], "imageInfo": [], "monitorId": 19058723386, "guid": "1084067920781733888", "parentGuid": "1084062244781113347", "engagementType": "RETWEET", "mediaUrls": ["https://video.twimg.com/ext_tw_video/1084060799595933698/pu/vid/640x480/bZ1hcR-mCViaG2vQ.mp4?tag=8"]}, {"url": "http://twitter.com/Sidar66187750/status/1084068273380093955", "title": "", "type": "Twitter", "location": "Diyarbakir, Southeastern Anatolian Region, TUR", "geolocation": {"id": "TUR.Southeastern Anatolian Region.Diyarbakir", "name": "Diyarbakir", "country": "TUR", "state": "Southeastern Anatolian Region", "city": "Diyarbakir"}, "language": "tr", "assignedCategoryId": 19058723388, "assignedEmotionId": 0, "categoryScores": [{"categoryId": 19058723389, "categoryName": "Basic Negative", "score": 0.02}, {"categoryId": 19058723388, "categoryName": "Basic Positive", "score": 0.94}, {"categoryId": 19058723391, "categoryName": "Basic Neutral", "score": 0.05}], "emotionScores": [], "imageInfo": [], "monitorId": 19058723386, "guid": "1084068273380093955", "parentGuid": "1083705784817643521", "engagementType": "RETWEET"}, {"url": "http://twitter.com/ahmetAcer14/status/1084068082673483776", "title": "", "type": "Twitter", "location": "Adana, Mediterranean Region, TUR", "geolocation": {"id": "TUR.Mediterranean Region.Adana", "name": "Adana", "country": "TUR", "state": "Mediterranean Region", "city": "Adana"}, "language": "und", "assignedCategoryId": 0, "assignedEmotionId": 0, "categoryScores": [], "emotionScores": [], "imageInfo": [], "monitorId": 19058723386, "guid": "1084068082673483776", "parentGuid": "1084054837183029249", "engagementType": "RETWEET", "documentsUrls": ["https://www.turkiyegazetesi.com.tr/yazarlar/fuat-ugur/606038.aspx", "https://twitter.com/gencosmansarper/status/1083956208309018625"]}, {"url": "http://twitter.com/xcV44s101gjyOn3/status/1084068442758631425", "title": "", "type": "Twitter", "location": "DZA", "geolocation": {"id": "DZA", "name": "Algeria", "country": "DZA"}, "language": "ar", "assignedCategoryId": 19058723391, "assignedEmotionId": 0, "categoryScores": [{"categoryId": 19058723389, "categoryName": "Basic Negative", "score": 0.02}, {"categoryId": 19058723388, "categoryName": "Basic Positive", "score": 0.35}, {"categoryId": 19058723391, "categoryName": "Basic Neutral", "score": 0.63}], "emotionScores": [], "imageInfo": [], "monitorId": 19058723386, "guid": "1084068442758631425", "parentGuid": "1084049278119612421", "engagementType": "RETWEET", "mediaUrls": ["http://pbs.twimg.com/media/DwtRDo0WsAER1-T.jpg", "http://pbs.twimg.com/media/DwtRCGuXQAAm35S.jpg", "http://pbs.twimg.com/media/DwtRC3KW0AA5RA3.jpg", "http://pbs.twimg.com/media/DwtRERfW0AA7sIN.jpg"]}, {"url": "http://twitter.com/Antaakya_Akgenc/status/1084066851242557440", "title": "", "type": "Twitter", "location": "Mediterranean Region, TUR", "geolocation": {"id": "TUR.Mediterranean Region", "name": "Mediterranean Region", "country": "TUR", "state": "Mediterranean Region"}, "language": "tr", "assignedCategoryId": 19058723391, "assignedEmotionId": 0, "categoryScores": [{"categoryId": 19058723389, "categoryName": "Basic Negative", "score": 0.37}, {"categoryId": 19058723388, "categoryName": "Basic Positive", "score": 0.17}, {"categoryId": 19058723391, "categoryName": "Basic Neutral", "score": 0.46}], "emotionScores": [], "imageInfo": [{"url": "http://pbs.twimg.com/media/DwthDPkX0AAxAWJ.jpg", "brands": [], "objects": [{"score": 0.89882, "classId": 3324, "className": "Demonstration/protest"}]}, {"url": "http://pbs.twimg.com/media/DwthDPkXQAAf_0t.jpg", "brands": []}, {"url": "http://pbs.twimg.com/media/DwthDPcXcAETe37.jpg", "brands": [], "objects": [{"score": 0.96125, "classId": 3119, "className": "Sitting"}, {"score": 0.83501, "classId": 3488, "className": "People"}]}], "monitorId": 19058723386, "guid": "1084066851242557440", "mediaUrls": ["http://pbs.twimg.com/media/DwthDPkX0AAxAWJ.jpg", "http://pbs.twimg.com/media/DwthDPkXQAAf_0t.jpg", "http://pbs.twimg.com/media/DwthDPcXcAETe37.jpg"]}, {"url": "http://twitter.com/Charles70067222/status/1084068597453017090", "title": "", "type": "Twitter", "language": "tr", "authorGender": "M", "assignedCategoryId": 19058723391, "assignedEmotionId": 0, "categoryScores": [{"categoryId": 19058723389, "categoryName": "Basic Negative", "score": 0.33}, {"categoryId": 19058723388, "categoryName": "Basic Positive", "score": 0.39}, {"categoryId": 19058723391, "categoryName": "Basic Neutral", "score": 0.27}], "emotionScores": [], "imageInfo": [], "monitorId": 19058723386, "guid": "1084068597453017090", "parentGuid": "1084068468910161920", "engagementType": "REPLY"}, {"url": "http://twitter.com/fbardia/status/1084068551059783681", "title": "", "type": "Twitter", "language": "es", "authorGender": "M", "assignedCategoryId": 19058723391, "assignedEmotionId": 0, "categoryScores": [{"categoryId": 19058723389, "categoryName": "Basic Negative", "score": 0.04}, {"categoryId": 19058723388, "categoryName": "Basic Positive", "score": 0.09}, {"categoryId": 19058723391, "categoryName": "Basic Neutral", "score": 0.87}], "emotionScores": [], "imageInfo": [], "monitorId": 19058723386, "guid": "1084068551059783681", "parentGuid": "1083916530784657408", "engagementType": "REPLY"}], "totalPostsAvailable": 16, "status": "success"}]

当我对这个 JSON 文件进行 Dataframe 时,它​​看起来像这样: enter image description here

我需要对“帖子”列中的字符串进行数据框化。我试过这段代码:

with open('test.json') as data_file:    
d = json.loads(data_file.read())
df= pd.DataFrame(d[0]['posts'])
df

df 仅带来第一行的值,或包含 (84 url​​s) 的索引 [0],如下图所示: enter image description here

我需要什么:

有没有办法将所有索引中的字典 Dataframe?

提前致谢!

最佳答案

我相信你需要json_normalize :

import json
from pandas.io.json import json_normalize

with open('test.json') as file:
j = json.load(file)

df = json_normalize(j, 'posts', ['totalPostsAvailable','status'])
print (df)

关于python - DataFrame 在列表中重复字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54160084/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com