# Get elements from lists in pandas dataframe [closed]

I have the following column of a data frame:

I get it by doing dataFrame['opcodes].

and I would like to consider only the first 20 and the last 20 elements of each list. Is there a way to do this?

I have tried to do the following:

dataFrame['opcodes_modified'] =  dataFrame['opcodes'].apply(lambda x: x[:20].append(x[-20:])


but i get the following error message:

SyntaxError: unexpected EOF while parsing


can somebody help me? Thank's in advance.

• Hi, this kind of question should be asked on StackOverflow. Having said this, don't you miss an axis=1 parameter in df.apply function? – Romain Reboulleau Oct 31 at 22:23

You logic is correct, it's just the lambda function that is slightly wrong. .append() adds a single element to the end of the list, so in your case I guess it adds an element which is a list of 20 elements. You could either use .extend() instead, which will do what you want or you can simply write
lambda x: x[:20] + x[-20:]