I have tried to find something that could help me but I couldn't. I'd appreciate if someone could link me to it, if my question is already answered.
I have a pandas dataframe that has row-wise features. For example:
Patient_ID Feature_Id Feature_Value
0 3 10 0.30
1 3 50 0.20
2 3 60 1.00
3 4 10 0.25
I need to convert them into column-wise features(essentially columns in Pandas) -- something like below:
Patient_Id 10 50 60
3 0.30 0.2 1.0
4 0.25 Nan Nan
You could try pd.pivot_table
In [16]: pd.pivot_table(df, index='Patient_ID', values='Feature_Value', columns='Feature_ID')
Out[16]:
Feature_ID 10 50 60
Patient_ID
3 0.30 0.2 1.0
4 0.25 NaN NaN
Note that if you need to specify what to do if more than a single entry exists (which you don't have in your example), you can use the aggfunc
parameter (the default is to calculate the mean).
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.