Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas Series difference between accessing values using string and nested list

Hi I'm still new to pandas method and I just to have a better understanding about the pd.Series

ser = pd.Series(data=[100,"200",300,"400",500],index=["tom","bob","nancy","dan","eric"])

I found out when ever I want to extract a value using the index of it for example

ser["nancy"]

the output will be

300

but if i use nested list

ser[["nancy"]]

I will get

nancy    300

I know it's maybe a simple thing but I just want to know the reason behind that to have a better understanding and if there is any resource you would recommend I would highly appreciate it

Thanks

like image 654
Khaled Khanfar Avatar asked Mar 05 '23 19:03

Khaled Khanfar


2 Answers

When you use:

ser["nancy"]
>> 300

it returns an integer

type(ser['nancy']
>> int

But when you use

ser[['nancy']]
>> nancy    300
dtype: object

It actually returns a series:

type(ser[['nancy']])
>> pandas.core.series.Series

So when you use a list, it always returns a series and not just the corresponding value as when you use just a string:

ser[["nancy", "dan"]]
>> nancy      300
dan    400
dtype: object
like image 155
Mohit Motwani Avatar answered May 09 '23 02:05

Mohit Motwani


When you do ser["nancy"] you signal pandas you want a single value, so it will return the value at the "nancy" index key. When you do ser[["nancy"]] you signal pandas you may want multiple values, so it returns a data structure (in this case a Series) that contains those (possible) multiple values. This is better illustrated in the following example:

import pandas as pd

ser = pd.Series(data=[100, "200", 300, "400", 500], index=["tom", "bob", "nancy", "dan", "eric"])

result = ser["nancy"]
print(result, type(result))

result = ser[["nancy", "dan"]]
print(result, type(result))

Output

300 <class 'int'>
nancy    300
dan      400
dtype: object <class 'pandas.core.series.Series'>

As you can see from the output, ser["nancy"] return the value of ser at "nancy", in this case 300, you can further verify that is a single value by it's type (int). For the second case the type of result is a Series.

like image 26
Dani Mesejo Avatar answered May 09 '23 04:05

Dani Mesejo