简体   繁体   中英

How do I create a Pivot table in Pandas using the default index as the pivot index?

I have the below data frame:

   Dataset Scores
0   times_pregnant  6.000
1   plasma_glucose_concentration    148.000
2   diastolic_blood_pressure    72.000
3   triceps_thickness   35.000
4   2_hour_serum_insulin    0.000
... ... ...
7828    BMI 30.400
7829    diabetes_pedigreen  0.315
7830    age 23.000
7831    diabetes    0.000
7832    times_pregnant  1.000

I want to turn the "Dataset" column contents into column headers and have the "Scores" column as values. When I try to pivot without specifying an index it just gives me the average of all the scores under each column header, but I want each score listed under each column header.

Current result:

Dataset 2_hour_serum_insulin    BMI age diabetes    diabetes_pedigreen  diastolic_blood_pressure    plasma_glucose_concentration    times_pregnant  triceps_thickness
Scores  79.799479   31.992578   33.240885   0.348958    0.471876    69.105469   120.894531  3.845052    20.536458

Desired result:

times_pregnant  BMI    Age
2               23.50  45
4               30.40  23
3               41.50  23
5               25.40  42

Pandas gives you a default unique index but I don't understand how to set this as my index to allow all my scores to be listed out.

Is pivoting the best way to do this?

Assuming each woman has the same amount of scores, you could add an index for each woman and pivot on it (df is your dataframe):

 u = df['Dataset'].nunique()
 df['women_idx'] = [int(x/u) for x in range(df.shape[0])]

 df.pivot(columns='Dataset', index='women_idx')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM