将字典转换为熊猫数据框

时间:2021-05-12 11:57:06

标签: python pandas dataframe

试过这个:

df = pd.DataFrame(list(sample.items()),columns = ['id','age','chest_pain_type',
        'blood_sugar','rest_electro','max_heart_rate', 'exercice_angina','rest_blood_pressure']) 

明白了: 8 列通过,通过的数据有 2 列 错误发生在这一行:

columns = _validate_or_indexify_columns(content, columns)

字典“样本”:

    {'age': '34', 'max_heart_rate': '123', 'rest_blood_pressure': '132',
     'blood_sugar': '1', 'exercice_angina': '0', 'chest_pain_type': 'typ_angina',
 'rest_electro': 'normal', 'id': 8808}

1 个答案:

答案 0 :(得分:0)

如果使用所有标量值,则必须传递索引

df = pd.DataFrame({'age': '34', 'max_heart_rate': '123', 'rest_blood_pressure': '132',
     'blood_sugar': '1', 'exercice_angina': '0', 'chest_pain_type': 'typ_angina',
 'rest_electro': 'normal', 'id': 8808},index = [0])


    age max_heart_rate  rest_blood_pressure blood_sugar exercice_angina chest_pain_type rest_electro    id
0   34    123                  132              1             0    typ_angina   normal  8808