1

First of all, I have created a function with input of lat Lon in order to filter ships not entering a particular zone.

check_devaiation_notInZone(LAT, LON)

It takes two inputs and return True if ships not enter a particular zone.

Secondly, I got data on many ships with Lat in one header and Lon in another header in CSV format. So, I need to take data from two column into the function and create another column to store the output of the function.

After I looked at Pandas: How to use apply function to multiple columns. I found the solution df1['deviation'] = df1.apply(lambda row: check_devaiation_notInZone(row['Latitude'], row['Longitude']), axis = 1)

But I have no idea why it works. Can anyone explain the things inside the apply()?

JOHN
  • 1,411
  • 3
  • 21
  • 41
  • For each row in `df1`, run `check_devaiation_notInZone` with values from the `Latitude` and `Longitude` columns, and save the result in the `deviation` column. `axis = 1` means to apply the function over each row instead of each column. – 0x5453 Dec 01 '17 at 13:43

1 Answers1

1

A lambda function is just like a normal function but it has no name and can be used only in the place where it is defined.

lambda row: check_devaiation_notInZone(row['Latitude'], row['Longitude'])

is the same as:

def anyname(row):
    return check_devaiation_notInZone(row['Latitude'], row['Longitude'])

So in the apply you just call another function check_devaiation_notInZone with the parameters row['Latitude'], row['Longitude'].

mrCarnivore
  • 4,638
  • 2
  • 12
  • 29
  • And why row['Latitude'], row['Longitude'] can refer to the columns? The function will take "row" as input, but what exactly is the input in this code: df1['deviation'] = df1.apply(lambda row: check_devaiation_notInZone(row['Latitude'], row['Longitude']), axis = 1) – JOHN Dec 02 '17 at 00:54