简体   繁体   English

如何将数据帧行应用到定义的函数中

[英]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 LAT2 LONG2长2
VKO VKO 55.600000 55.600000 37.267778 37.267778
MLE MLE 4.184722 4.184722 73.518889 73.518889
DWC数据中心 24.916944 24.916944 55.168056 55.168056

You can use .apply() with axis=1 to apply the function on the rows, as follows:您可以使用.apply()axis=1将函数应用于行,如下所示:

(assuming df is the name of your dataframe`): (假设df是您的数据框的名称):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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