remove last element of list within a Series

Viewed 14

I've tried different variations of pop, remove, and del with no success thus far. I'm not finding any documentation that addresses this particular issue. Thanks in advance

listoflists = [['a', 'b', 'c', 'd'],['e','f','g','h']]

series1 = pd.Series(listoflists)

->

0      [a, b, c, d]
1      [e, f, g, h]
dtype: object

I want to remove the last element of each list within the series to get:

0      [a, b, c]
1      [e, f, g]
1 Answers

You can use .apply() with lambda:

series1.apply(lambda x: x[:-1])

From each list we take all elements except of the last one.

Output:

0    [a, b, c]
1    [e, f, g]
Related