Exploring Data with pandas: Fundamentals (Page 12)

sector_china = f500["sector"][f500["country"] == "China"].value_counts().head(3)
sector_china = f500["sector"][f500["country"] == "China"]
sector_china.value_counts().head(3)

Question: Why does that spliting the code into 2 (below) leads to a different result? Appreciate your kind attention!

The difference is in the variable assignment.

sector_china = f500["sector"][f500["country"] == "China"].value_counts().head(3)

In this first set of code, we getting the value_counts() of this section of the data, getting the top 3, and then assigning that as the variable sector_china.

sector_china = f500["sector"][f500["country"] == "China"]
sector_china.value_counts().head(3)

In this second set of code, we are assigning the series of the sectors in China to the variable sector_china. The second line is where we display those top 3 sectors, but the variable contains the whole series.

I hope that makes sense!

2 Likes