anonymous
anonymous

Reputation: 189

How `pandas.Series.str` works when values are `list`?

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

Answers (1)

jezrael
jezrael

Reputation: 862511

It working, because strings and lists 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

Related Questions