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

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

解析dataframe中json類型格式的元素

解析dataframe中json類型格式的元素

Smart貓小萌 2023-04-11 14:42:39
我有這個帶有大地水準面的數(shù)據(jù)框,看起來像這樣我想要做的是將每個 msaid 的每個大地水準面編號放入列表中。理想情況下,我希望有一個看起來像這樣的數(shù)據(jù)框我希望這是有道理的。任何幫助,將不勝感激。這里有兩個例子:159 [{"geoid":"02020000101"},{"geoid":"02020000204"},{"geoid":"02020000300"},{"geoid":"02020000400"},{"geoid":"02020000500"},{"geoid":"02020000600"},{"geoid":"02020000802"},{"geoid":"02020000901"},{"geoid":"02020000902"},{"geoid":"02020001000"},{"geoid":"02020001500"},{"geoid":"02020001601"},{"geoid":"02020001602"},{"geoid":"02020001701"},{"geoid":"02020001802"},{"geoid":"02020001900"},{"geoid":"02020002000"},{"geoid":"02020002100"},{"geoid":"02020002201"},{"geoid":"02020002400"},{"geoid":"02020002501"},{"geoid":"02020002502"},{"geoid":"02020002601"},{"geoid":"02020002712"},{"geoid":"02020002811"},{"geoid":"02020002812"},{"geoid":"02020002813"},{"geoid":"02122000100"},{"geoid":"02122000300"},{"geoid":"02170001300"},{"geoid":"02170000300"},{"geoid":"02170001100"},{"geoid":"02170000800"},{"geoid":"02261000300"},{"geoid":"02290000400"},{"geoid":"02240000400"},{"geoid":"02170000102"},{"geoid":"02170000402"},{"geoid":"02170000101"},{"geoid":"02170001201"},{"geoid":"02170001001"},{"geoid":"02170000706"},{"geoid":"02170001202"},{"geoid":"02170001004"},{"geoid":"02170000705"},{"geoid":"02170000603"},{"geoid":"02020000102"},{"geoid":"02020000201"},{"geoid":"02020000202"},{"geoid":"02020000203"},{"geoid":"02020000701"},{"geoid":"02020000702"},{"geoid":"02020000703"},{"geoid":"02020000801"},{"geoid":"02020001100"},{"geoid":"02020001200"},
查看完整描述

2 回答

?
翻過高山走不出你

TA貢獻1875條經(jīng)驗 獲得超3個贊

我下載了該文件并將其作為 csv 文件保存在我的計算機中。然后我運行了以下代碼。


import pandas as pd

df = pd.read_csv('parse_this.csv')

#remove characters and convert to list

df.tracts = df.tracts.apply(lambda x: x.strip('][').split(','))

#explode tracts series

df = df.explode('tracts')

#resetting index and renaming columns

df.reset_index(drop = True, inplace = True)

df.rename(columns={"tracts": "geoid"} , inplace = True)

#removing extra characters to keep only the geoid number

df.geoid = df.geoid.apply(lambda x: x.strip('geoid{}:""'))

df


查看完整回答
反對 回復(fù) 2023-04-11
?
江戶川亂折騰

TA貢獻1851條經(jīng)驗 獲得超5個贊

我希望這個例子有幫助:


#creating a dataframe for example:


d = [{'A':3,'B':[{'id':'001'},{'id':'002'}]},

    {'A':4,'B':[{'id':'003'},{'id':'004'}]},

    {'A':5,'B':[{'id':'005'},{'id':'006'}]},

    {'A':6,'B':[{'id':'007'},{'id':'008'}]}]

df = pd.DataFrame(d)

df

    A   B

0   3   [{'id': '001'}, {'id': '002'}]

1   4   [{'id': '003'}, {'id': '004'}]

2   5   [{'id': '005'}, {'id': '006'}]

3   6   [{'id': '007'}, {'id': '008'}]


#apply an explode to the column B and reset index


df = df.explode('B')

df.reset_index(drop = True, inplace = True)

df


# now it looks like this

    A    B

0   3   {'id': '001'}

1   3   {'id': '002'}

2   4   {'id': '003'}

3   4   {'id': '004'}

4   5   {'id': '005'}

5   5   {'id': '006'}

6   6   {'id': '007'}

7   6   {'id': '008'}


# now we need to remove the extra text and rename the column from B to id

df.B = df.B.apply(lambda x: x['id'])

df.rename(columns={"B": "id"} , inplace = True)


# this is the final product:

df

    A   id

0   3   001

1   3   002

2   4   003

3   4   004

4   5   005

5   5   006

6   6   007

7   6   008


查看完整回答
反對 回復(fù) 2023-04-11
  • 2 回答
  • 0 關(guān)注
  • 153 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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