[英]Issue in conversion of nested dictionary to dataframe pandas python
我有嵌套字典形式的數據,如下所示
data = {
"policy": {
"1": {
"ID": "ML_0",
"URL": "www.a.com",
"Text": "my name is Martin and here is my code"
},
"2": {
"ID": "ML_1",
"URL": "www.b.com",
"Text": "my name is Mikal and here is my code"
}
}
}
我正在嘗試使用以下代碼將其轉換為一種數據框。
for policies in data['policy']:
new = pd.DataFrame.from_dict(data['policy'], orient='index')
我明白了
ID URL Text
1 ML_0 www.a.com my name is Martin and here is my code
2 ML_1 www.b.com my name is Mikal and here is my code
實際上,在將字典轉換為數據框時,我不希望包含數字 1 和 2 的第一列。 任何人都可以幫助我,請。 謝謝
使用set_index
for policies in data['policy']:
new = pd.DataFrame.from_dict(data['policy'], orient='index').set_index('ID')
有關更多信息和選項,請參閱以下文檔
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.set_index.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.