from lesson 03_Data_ Sources_&_Transformations
The example code begins:
from math import pi
import pandas as pd
- Suddenly we are explicitly importing pandas. Why? The tutorial has used DataFrames in earlier lessons without this import, why now? What’s different?
I understand what this line of code is doing. I don’t understand how:
data = pd.Series(x).reset_index(name='value').rename(columns={'index':'country'})
It looks like the author couldn’t make up his/her mind about the index. That’s not what’s happening, but that’s why I find it confusing.
- It creates a Series
- The Series is split into two columns
- Which simultaneously makes a df
- The index is reset to the default ints.
- A color column was added.
BUT: Assuming these operations happen in a right to left sequence:
rename(columns={'index':'country'})
There is no column called index
If there is an index, then it is not a column, I’ve seen that error enough times to know that.
reset_index(name='value')
No column, or row for that matter, in the Series has any name at all, let alone one called ‘value’.
At no time is ‘value’ the index of anything in this sequence of operations.
reset_index puts us at the default integer index. What’s ‘value’ got to do with any of that?
Thx for both clarifying and elucidating.