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
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With