Panda : getting a dataframe instead of a Series on ".loc" method

My Code:

rank = f500['rank']
rank_desc = rank.describe()
prev_rank = f500.loc[:,['previous_rank']]
prev_rank_desc = prev_rank.describe()

I was expecting f500.loc[:,[‘previous_rank’]] to return a Series type object, but it returned a DataFrame

This seems odd to me, certainly I have missed something in the previous lectures explanations but I can’t seem to recall any differences pointed out about both method returning different data type.

I’m a bit confused, since I pass in an argument about a single column, shouldn’t the .loc method return a Series ? Why ?

Thanks for the help !
Peace, B.

Passing list of columns (['previous_rank']) will return a DataFrame, to get a Series, you need to pass only the column name 'previous_rank'


rank = f500['rank']
rank_desc = rank.describe()
prev_rank = f500.loc[:, 'previous_rank']
prev_rank_desc = prev_rank.describe()

If you are taking a pandas Series you do not need a square bracket. If you are taking a dataframe: combination of series, use a square bracket.
This should do it:


1 Like

Could you provide the line with f500?

Or at least f500.head(2)?

.loc is a short for “locate”
.iloc means: “integer locate”, i.e.based on specified integer

So, for

f500 - some pd.DataFrame

f500.iloc[:,0] - get all rows for columns with index 0 (will return a pd.Series)
f500['previous_rank'] - get the column with name “previous_rank” (a pd.Series) (just like @monorienaghogho wrote above)

1 Like

Gah, simple as that, thanks !

Thank you for your reply ! I really wanted to go through the explicit syntax there :slight_smile: