df['opposition'].apply(lambda x: x[2:])
Please help me to understand the lambda function and how it works in this case.
Few things at play here:
df[column].apply(f)takes a function
fas argument and applies that function to every value in
column, returning the new column with modified values.
lambda x: x[2:]defines a function that takes a value
xand returns the slice
x[2:]. I.e., when
xis a string, it returns
xwithout the first two characters.
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:
The methods in
.str are specific for columns with string values. See here for more info.