第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何將json文件中的特定鍵插入Python中的數(shù)據(jù)框中

如何將json文件中的特定鍵插入Python中的數(shù)據(jù)框中

手掌心 2022-06-22 15:38:02
如果這非常簡(jiǎn)單或已經(jīng)被問(wèn)到,我是 Python 新手并且使用 json 文件,所以我很困惑。我有一個(gè)從網(wǎng)站上抓取的 9 GB json 文件。這些數(shù)據(jù)包含大約 300 萬(wàn)人的信息。每個(gè)人都有屬性,但并不是所有的人都具有相同的屬性。一個(gè)屬性對(duì)應(yīng)于 json 文件中的一個(gè)鍵,如下所示:{  "_id": "in-00000001",  "name": {    "family_name": "Trump",    "given_name": "Donald"  },  "locality": "United States",  "skills": [    "Twitter",    "Real Estate",    "Golf"     ],  "industry": "Government",  "experience": [  {    "org": "Republican",    "end": "Present",    "start": "January 2017",    "title": "President of the United States"  },  {    "org": "The Apprentice",    "end": "2015",    "start": "2003",    "title": "The guy that fires people"  }]}所以在這里,、 、 、_id和name是locality屬性(鍵)。另一個(gè)配置文件可能具有其他屬性,例如、、,或者缺少在另一個(gè)配置文件中找到的某些屬性,例如屬性等。skillsindustryexperienceeducationawardsinterestsskills我想做的是掃描 json 文件中的每個(gè)配置文件,如果配置文件包含屬性和skills,我想提取該信息并將其插入數(shù)據(jù)框中(我想我需要 Pandas ?)。從中,我想具體提取他們當(dāng)前雇主的姓名,即最近在. 數(shù)據(jù)框如下所示:industryexperienceexperienceorg    Industry   | Current employer | Skills    ___________________________________________________________________    Government | Republican       | Twitter, Real Estate, Golf    Marketing  | Marketers R Us   | Branding, Social Media, Advertising...對(duì)于具有這三個(gè)屬性的所有配置文件,依此類推。我正在努力尋找一個(gè)很好的資源來(lái)解釋如何做這種事情,因此我的問(wèn)題是。我想粗略的偽代碼是:for each profile in open(path to .json file):    if profile has keys "experience", "industry" AND "skills":        on the same row of the data frame:            insert current employer into "current employer" column of             data frame            insert industry into "industry" column of data frame            insert list of skills into "skills" column of data frame我只需要知道如何用 Python 編寫(xiě)它。
查看完整描述

1 回答

?
炎炎設(shè)計(jì)

TA貢獻(xiàn)1808條經(jīng)驗(yàn) 獲得超4個(gè)贊

我假設(shè)該文件包含所有配置文件,例如


{

    "profile 1" : {

        # Full object as in the example above

    },

    "profile 2" : {

        #Full object as in the example above

    }

}

在繼續(xù)之前,讓我展示一個(gè)使用 Pandas DataFrames 的正確方法。


更好地使用 Pandas DataFrames 的示例:

Pandas DataFrame 中的值不能是列表。因此,我們將不得不復(fù)制行,如下例所示。查看此問(wèn)題和 JD Long 的答案以獲取更多詳細(xì)信息:如何在 pandas 數(shù)據(jù)框中使用列表作為值?


ID      |    Industry   | Current employer | Skill

___________________________________________________________________

in-01   |    Government | Republican       | Twitter

in-01   |    Government | Republican       | Real Estate

in-01   |    Government | Republican       | Golf

in-02   |    Marketing  | Marketers R Us   | Branding

in-02   |    Marketing  | Marketers R Us   | Social Media

in-02   |    Marketing  | Marketers R Us   | Advertising

在以下代碼的注釋中查找解釋:


import json

import pandas as pd


# Create a DataFrame df with the columns as in the example

df = pd.DataFrame(data, columns = ['ID', 'Industry','Employer','Skill']) 


#Load the file as json. 

with open(path to .json file) as file:

    #readlines() reads the file as string and loads() loads it into a dict

    obj = json.loads(''.join(file.readlines()))

    #Then iterate its items() as key value pairs

    #But the line of code below depends on my first assumption.

    #Depending on the file format, the line below might have to differ.

    for prof_key, profile in obj.items():

        # Verify if a profile contains all the required keys

        if all(key in profile.keys() for key in ("_id","experience", "industry","skills")):

            for skill in profile["skills"]:

                df.loc[-1] = [profile["_id"],

                              profile["industry"],

                              [x for x in profile["experience"] if x["end"] == "Present"][0]["org"],

                              skill]

上面的行在df.loc[-1] = ...數(shù)據(jù)框中插入一行作為最后一行(索引-1)。


當(dāng)您稍后希望使用此信息時(shí),您將不得不使用df.groupby('ID')


讓我知道您的文件中是否有不同的格式,以及此說(shuō)明是否足以讓您入門(mén)或您需要更多。


查看完整回答
反對(duì) 回復(fù) 2022-06-22
  • 1 回答
  • 0 關(guān)注
  • 119 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)