2
$\begingroup$

I have the following column of a data frame:

enter image description here

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.

$\endgroup$
  • $\begingroup$ 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? $\endgroup$ – Romain Reboulleau Oct 31 at 22:23
2
$\begingroup$

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:]
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.