I have a json file which when converted into a dataframe looks something like this:
sl no. id name value date
1 101 Math 90 -
2 101 Phy 87 -
3 201 Math 85 -
4 201 Phy 93 -
(Not the actual data but of the same principle with multiple repeating entries with one differing category)
What I’m trying to achieve looks something like this:
sl no. id Math Phy date
1 101 90 87 -
...
Is there any way to easily convert these categorical entries into columns of the same name instead of having multiple repeating entries for each student?
2
Answers
Yes, you can achieve this using the
pivot_table
function in pandas. Here’s a sample code snippet:This will create a DataFrame where each unique value in the ‘name’ column becomes a separate column, with corresponding ‘value’ entries under each student’s ‘id’.
You can use pandas.pivot() to do this. Assuming id is the column you want to use for each row, this will be your
index
(you can pass a list if there are other columns to be used here).name
is the column to turn into the columns, andvalues
specifies the data to show in the new pivoted dataframe. Which looks like this:Which returns:
(Assuming you want to keep both the maths and phy dates)
To remove the multi-index columns, you can rename them using:
Giving: