Question: Trying to understand someone else's code. Can someone please explain what the lambda function is doing here?. Does the lambda function here translates to: If the first 3 digits of OrderNumber
are not 486
and not 561
, and the first digit is not 8
then set the column value data_df[OrderNumber]
of the dataframe
to empty string; otherwise leave it as it is?
import sqlalchemy as sq
import pandas as pd
data_df = pd.read_csv('/dbfs/FileStore/tables/CustomerOrders.txt', sep=',', low_memory=False, quotechar='"', header='infer' , encoding='cp1252')
data_df[OrderNumber] = data_df[OrderNumber].apply(lambda x: x if x[:3] != '486' and x[:3] != '561' and x[:1] != '8' else "")
.............
.............
CodePudding user response:
lambda x: x if x[:3] != '486' and x[:3] != '561' and x[:1] != '8' else ""
essentially translates to:
if x[:3] != '486' and x[:3] != '561' and x[:1] != '8':
return x
else:
return ""
CodePudding user response:
You have it backwards if the condition evaluates to True (which will occur in the instances you describe) the entry will be unchanged, if any of those equalities are met the condition evaluates to False and the entry is made to the empty string.
CodePudding user response:
It filtered all orders which OrderNumber startswith ('486', '561', '8')
and set their number to empty string ""
I guess these OrderNumbers are not valid or not useful for current situation.