Working with df.pivot() method

Hello :smiley:

Below I’ve posted my code to find out how df.pivot() method works. This is related to Transforming Data With Pandas. This project was exciting and kind of crazy because I wished to create an answer that will unfold from the whole Transforming Data With Pandas lesson. Because the additional task about finding out how df.pivot() method works was the last step of this lesson, I desired to add all preceding steps to the code. Why? Well, I thought that maybe it will be cool to have all things in one place, what’s more, I could use some objects from this lesson in the final df.pivot() “part”.

I thought it will take maybe a few hours and will be done. I was so wrong! It was so because when I’ve started to create this code, almost immediately another problem arises. An old problem that bothers me almost from the beginning of my learning: when someone wants to see the code without downloading it: no pictures are displayed. Fixing that took me 3 weeks.

Full code below:

Transforming Data With Pandas - df.pivot() method good version.ipynb (263.6 KB)


Click here to view the jupyter notebook file in a new tab

About the df.pivot(). I don’t understand one thing. The object created with df.pivot() is a dataframe, but when it includes two columns I can’t choose in any way to display single cell, or row slice. All I can do is posted below. I don’t know why it is so.

image

image

why this doesn’t works ? :

2 Likes

Hi, Thanks for sharing! I think I may have an answer to your problem with the indexing. You have to use .iloc to specify one or more rows of the dataframe. For example,

print(economy_10.iloc[0:3]) gives the first three entries:

variable Economy
1.25018 New Zealand
1.29025 Finland
1.30232 Iceland

Also, I noticed that when you created economy_10, you set index = 'value'. So now your indexes are the floats in the value column, which you can verify with:

print(economy_10.index)

Float64Index([1.2501799999999998, 1.29025, 1.3023200000000001, 1.32548, 1.32629, 1.32944, 1.33171, 1.33358, 1.3965100000000001, 1.459]

So you can also access by the index “names” using .loc, for example,

print(economy_10.loc[1.32629:1.459]) gives

variable Economy
1.32629 Canada
1.32944 Netherlands
1.33171 Sweden
1.33358 Australia
1.39651 Switzerland
1.45900 Norway

I’m wondering, because these float indices seem awkward to me, but I’m new to this. You can create a dataframe with the same info, but with integer indices using new_melt[['value', 'Country']]. Is there a particular reason you chose your format?