我正在嘗試按照以下示例使用爆炸:#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 B0 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 indexdf1 = df.explode('B')df1.reset_index(drop = True, inplace = True)df1# now it looks like this A B0 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'}我的數據看起來像這樣,非常相似:msaid tracts0 159 [{"geoid":"02020000101"},{"geoid":"02020000204...1 160 [{"geoid":"26091060100"},{"geoid":"26125138100...2 161 [{"geoid":"01115040300"},{"geoid":"01015001700...3 163 [{"geoid":"72054580100"},{"geoid":"72054580200...4 162 [{"geoid":"55135100200"},{"geoid":"55135101200...問題是當我應用時,df.explode('tracts')數據框沒有任何變化,我不確定為什么。非常感謝任何建議。這是我上面后者的代碼:df = pd.read_excel('parse this.xlsx')df.head() msaid tracts0 159 [{"geoid":"02020000101"},{"geoid":"02020000204...1 160 [{"geoid":"26091060100"},{"geoid":"26125138100...2 161 [{"geoid":"01115040300"},{"geoid":"01015001700...3 163 [{"geoid":"72054580100"},{"geoid":"72054580200...4 162 [{"geoid":"55135100200"},{"geoid":"55135101200...
2 回答

喵喵時光機
TA貢獻1846條經驗 獲得超7個贊
使用ast模塊將字符串轉換為列表對象,然后使用explode
前任:
import ast
data = [{'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(data)
df["B"] = df['B'].apply(ast.literal_eval)
df1 = df.explode('B')
df1.reset_index(drop = True, inplace = True)
print(df1)
輸出:
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'}

明月笑刀無情
TA貢獻1828條經驗 獲得超4個贊
您需要更改類型以列出,然后您可以使用爆炸。
df=df.assign(**df['tracts'].apply(eval)).explode('tracts')
添加回答
舉報
0/150
提交
取消