Reputation: 189
How this works? If it's converting each item into string i should get first character like "[1,2]"[0]
----> '['
but instead it treating as list
, How?
>>> df = pd.DataFrame({'a':[[1,2],[2,3]]})
>>> df.a.str[0]
0 1
1 2
Name: a, dtype: int64
Upvotes: 2
Views: 109
Reputation: 862511
It working, because string
s and list
s are iterables. It means .str[0]
return first value of string, first value of lists, first value of iterable.
What is iterable
:
As stated by Raymond Hettinger:
An ITERABLE is:
- anything that can be looped over (i.e. you can loop over a string or file) or
- anything that can appear on the right-side of a for-loop:
for x in iterable: ...
or- anything you can call with
iter()
that will return an ITERATOR:iter(obj)
or- an object that defines
__iter__
that returns a fresh ITERATOR, or it may have a__getitem__
method suitable for indexed lookup.
Upvotes: 3