我有一个复杂的表,我想转换表。 我的桌子:
d3 = [{"year_old":25, "sex": 'male', "clinic_1": 1, "clinic_2":2,
"clinic_3": 3, "clinic_4":4}, {"year_old":25, "sex": 'female',
"clinic_1": 5, "clinic_2":6, "clinic_3": 7, "clinic_4":8},
{"year_old":26, "sex": 'male', "clinic_1": 9, "clinic_2":10,
"clinic_3": 11, "clinic_4":12}]
df3 = pd.DataFrame(d3)
print(df3)
clinic_1 clinic_2 clinic_3 clinic_4 sex year_old
0 1 2 3 4 male 25
1 5 6 7 8 female 25
2 9 10 11 12 male 26
我想看到的桌子是
clinic count_people sex year_old
0 clinic_1 1 male 25
1 clinic_1 5 female 25
2 clinic_1 9 male 26
3 clinic_2 2 male 25
4 clinic_2 6 female 25
5 clinic_2 10 male 26
我尝试以下代码:
pnew = pd.DataFrame() for column in df3.columns[2:]:
for cnt in df3[column]:
pnew = pnew.append(df3[column],df3['sex'],df3['year_old'])
但是此代码会产生错误:
ValueError:系列的真值不明确。使用空 a.bool(),a.item(),a.any()或a.all()。
我不确定这段代码是否正确