ℕʘʘḆḽḘ
ℕʘʘḆḽḘ

Reputation: 19395

rotating a list using Pandas only

We are all familiar with the classic question of rotating a list to the left, say:

mylist = [1,2,3,4,5,6,7]

#rotate by two to the left
mylist[2:] + mylist[:2]

Out[4]: [3, 4, 5, 6, 7, 1, 2]

Now consider the same list, as a Pandas series.

pdlist = pd.Series(mylist)
Out[8]: 
0    1
1    2
2    3
3    4
4    5
5    6
6    7
dtype: int64

How can I rotate it in the same way, by only using Pandas functions?

Thanks!

Upvotes: 1

Views: 53

Answers (1)

Use:

pdlist[2:].append(pdlist[:2])

Upvotes: 1

Related Questions