简体   繁体   中英

how to apply dataframe rows into a defined function

I have a function so that I could add a name and its lats and longs and at first I add them line by line

 `def add_coordinate():
         # Coordinate location name
    name: str
         # Longitude
    longitude: Numeric
         # Latitude
    latitude: Numeric
`
geo = Geo()
 
# Add coordinate point, add name and latitude and longitude
geo.add_coordinate(name="VKO",longitude=55.60,latitude=37.26)
geo.add_coordinate(name="MLE",longitude=4.18,latitude=73.52)

Now I have a dataframe with the data I wanted to add, how could I loop the dataframe to apply these rows to the function?

IATA LAT2 LONG2
VKO 55.600000 37.267778
MLE 4.184722 73.518889
DWC 24.916944 55.168056

You can use .apply() with axis=1 to apply the function on the rows, as follows:

(assuming df is the name of your dataframe`):

df.apply(lambda x: geo.add_coordinate(name=x.IATA, longitude=x.LONG2, latitude=x.LAT2), axis=1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM