df['opposition'].apply(lambda x: x[2:])
Please help me to understand the lambda function and how it works in this case.
Advertisement
Answer
Few things at play here:
df[column].apply(f)takes a functionfas argument and applies that function to every value incolumn, returning the new column with modified values.lambda x: x[2:]defines a function that takes a valuexand returns the slicex[2:]. I.e., whenxis a string, it returnsxwithout the first two characters.- Hence,
df['opposition'].apply(lambda x: x[2:])returns the'opposition'column modified by removing the first 2 characters from all strings in it.
However, for this particular use case, there is a much better way to do this. You can use .str.slice() to perform the same operation:
df['opposition'].str.slice(start=2)
The methods in .str are specific for columns with string values. See here for more info.