Python pandas 'correct' syntax for slicing an entire column's/series values

Viewed 51

Given:

pandas.Series([[1,2],[3,4]])
0    [1, 2]
1    [3, 4]
dtype: object

Using str namespace of Dataframe/Series its possible to get a slice of the values (rather than of the dataframe) just like slice does. If I understand correctly that is because slice is actually used on each element. so it makes sense:

pandas.Series([[1,2],[3,4]]).str.slice(1,2)
0    [2]
1    [4]
dtype: object

The thing is I expected a different syntax to perform this kind of slicing. something more meaningful, like:
pandas.Series([[1,2],[3,4]]).iterable.slice(1,2)
or
pandas.Series([[1,2],[3,4]]).slice(1,2)

Is there an alternative syntax to apply this sort of slice that's more "intuitive" rather than looking like it is "casting" the data into strings?

1 Answers

You can try:

pandas.Series([[1,2],[3,4]]).map(lambda x:x[1:2])

which tells pandas to slice each element with it's native syntax

Related