Skip to content
Advertisement

How to create pivot table from a pandas dataframe having string data types in the correct order

I have a dataframe which looks like below,

enter image description here

Here is the same data in table format which you can copy/paste,

JavaScript

Here is the same data in dictionary format which you can copy/paste,

JavaScript

I tried using below code, but some of the SourceName was having wrong type, eg ‘peptide magainin’ should be a CHEMICAL, but it comes under DISEASE which is incorrect.

JavaScript

Sample output which is incorrect, can someone help me with this, thanks.

enter image description here

Expected output:

enter image description here

Advertisement

Answer

I don’t understand exactly what you try to achieve with the new structure, but it can be done by grouping once by “SourceType” and once by “TargetType”, then merging the resulting dataframes:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement