Pandas Series difference between accessing values using string and nested list
![Multi tool use Multi tool use](http://sgv.ssvwv.com/sg/ssvwvcomimagb.png)
Multi tool use
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
python pandas series
add a comment |
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
python pandas series
add a comment |
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
python pandas series
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
python pandas series
python pandas series
edited Jan 7 at 16:33
![](https://i.stack.imgur.com/awFXl.jpg?s=32&g=1)
![](https://i.stack.imgur.com/awFXl.jpg?s=32&g=1)
Mohit Motwani
1,7131522
1,7131522
asked Jan 1 at 12:24
![](https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg?sz=32)
![](https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg?sz=32)
Khaled KhanfarKhaled Khanfar
354
354
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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
add a comment |
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.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53995406%2fpandas-series-difference-between-accessing-values-using-string-and-nested-list%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
add a comment |
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
add a comment |
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 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
edited Jan 2 at 10:24
answered Jan 1 at 12:34
![](https://i.stack.imgur.com/awFXl.jpg?s=32&g=1)
![](https://i.stack.imgur.com/awFXl.jpg?s=32&g=1)
Mohit MotwaniMohit Motwani
1,7131522
1,7131522
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Jan 1 at 12:32
![](https://i.stack.imgur.com/CXDU3.jpg?s=32&g=1)
![](https://i.stack.imgur.com/CXDU3.jpg?s=32&g=1)
Daniel MesejoDaniel Mesejo
18.6k21432
18.6k21432
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53995406%2fpandas-series-difference-between-accessing-values-using-string-and-nested-list%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
uEQ skyu,hCc1wA0dzSZNW72aje6tSfa1GkFdwX,HN3ddZ6F,dKxd3F,qFz2 pBjBp1WIIeOuPx zo XMPb