将pandas列传递给函数时,“ ValueError:系列的真值不明确”

时间:2019-06-15 02:12:53

标签: python pandas dataframe valueerror

我想根据其他4列的值创建一个boolean类型的新列。我有一个函数is_proximal,它需要两个2元组(4个值)并返回一个布尔值。

我正在将列从pandas DataFrame传递给此函数。 is_proximal函数依次使用参数调用geopy.distance.distance

def is_proximal(p1, p2, exact=True):
    dist = distance(p1, p2)

    if exact:
        return dist.miles < 0.75  # mile threshold

    return dist.m < 100  # meter threshold



airbnb_coords = (df.loc[:, "lat_airbnb"], df.loc[:, "long_airbnb"])
homeaway_coords = (df.loc[:, "lat_homeaway"], df.loc[:, "long_homeaway"])
exacts.loc[:, "proximal"] = is_proximal(airbnb_coords, homeaway_coords)

这会导致以下错误:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

我无法理解为什么会发生此错误。我需要做些什么改变才能完成我想做的事情?

预期输出是boolean类型的附加列。输入数据帧df在所有列中都包含整数值。

完整的追溯:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-663-435de26b3cfa> in <module>
----> 1 m = filter_geographic_proximity(beds)

~/src/exemption_project/src/match.py in filter_geographic_proximity(df)
     53     airbnb_coords = (exacts.loc[:, "lat_airbnb"], exacts.loc[:, "long_airbnb"])
     54     homeaway_coords = (exacts.loc[:, "lat_homeaway"], exacts.loc[:, "long_homeaway"])
---> 55     exacts.loc[:, "proximal"] = is_proximal(airbnb_coords, homeaway_coords)
     56 
     57     airbnb_coords = (inexacts.loc[:, "lat_airbnb"], inexacts.loc[:, "long_airbnb"])

~/src/exemption_project/src/match.py in is_proximal(p1, p2, exact)
     29 def filter_geographic_proximity(df):
     30     def is_proximal(p1, p2, exact=True):
---> 31         dist = distance(p1, p2)
     32 
     33         if exact:

~/.local/share/virtualenvs/exemption_project-xI6bzvA1/lib/python3.7/site-packages/geopy/distance.py in __init__(self, *args, **kwargs)
    387         kwargs.pop('iterations', 0)
    388         major, minor, f = self.ELLIPSOID
--> 389         super(geodesic, self).__init__(*args, **kwargs)
    390 
    391     def set_ellipsoid(self, ellipsoid):

~/.local/share/virtualenvs/exemption_project-xI6bzvA1/lib/python3.7/site-packages/geopy/distance.py in __init__(self, *args, **kwargs)
    162         elif len(args) > 1:
    163             for a, b in util.pairwise(args):
--> 164                 kilometers += self.measure(a, b)
    165 
    166         kilometers += units.kilometers(**kwargs)

~/.local/share/virtualenvs/exemption_project-xI6bzvA1/lib/python3.7/site-packages/geopy/distance.py in measure(self, a, b)
    408     # Call geographiclib routines for measure and destination
    409     def measure(self, a, b):
--> 410         a, b = Point(a), Point(b)
    411         lat1, lon1 = a.latitude, a.longitude
    412         lat2, lon2 = b.latitude, b.longitude

~/.local/share/virtualenvs/exemption_project-xI6bzvA1/lib/python3.7/site-packages/geopy/point.py in __new__(cls, latitude, longitude, altitude)
    163                     )
    164                 else:
--> 165                     return cls.from_sequence(seq)
    166 
    167         if single_arg:

~/.local/share/virtualenvs/exemption_project-xI6bzvA1/lib/python3.7/site-packages/geopy/point.py in from_sequence(cls, seq)
    403             raise ValueError('When creating a Point from sequence, it '
    404                              'must not have more than 3 items.')
--> 405         return cls(*args)
    406 
    407     @classmethod

~/.local/share/virtualenvs/exemption_project-xI6bzvA1/lib/python3.7/site-packages/geopy/point.py in __new__(cls, latitude, longitude, altitude)
    176 
    177         latitude, longitude, altitude = \
--> 178             _normalize_coordinates(latitude, longitude, altitude)
    179 
    180         self = super(Point, cls).__new__(cls)

~/.local/share/virtualenvs/exemption_project-xI6bzvA1/lib/python3.7/site-packages/geopy/point.py in _normalize_coordinates(latitude, longitude, altitude)
     57 
     58 def _normalize_coordinates(latitude, longitude, altitude):
---> 59     latitude = float(latitude or 0.0)
     60     longitude = float(longitude or 0.0)
     61     altitude = float(altitude or 0.0)

~/.local/share/virtualenvs/exemption_project-xI6bzvA1/lib/python3.7/site-packages/pandas/core/generic.py in __nonzero__(self)
   1476         raise ValueError("The truth value of a {0} is ambiguous. "
   1477                          "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
-> 1478                          .format(self.__class__.__name__))
   1479 
   1480     __bool__ = __nonzero__

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

1 个答案:

答案 0 :(得分:1)

从回溯中可以明显看出,<?php return [ /* |-------------------------------------------------------------------------- | Mail Driver |-------------------------------------------------------------------------- | | Laravel supports both SMTP and PHP's "mail" function as drivers for the | sending of e-mail. You may specify which one you're using throughout | your application here. By default, Laravel is setup for SMTP mail. | | Supported: "smtp", "sendmail", "mailgun", "mandrill", "ses", | "sparkpost", "log", "array" | */ 'driver' => env('MAIL_DRIVER', 'smtp'), /* |-------------------------------------------------------------------------- | SMTP Host Address |-------------------------------------------------------------------------- | | Here you may provide the host address of the SMTP server used by your | applications. A default option is provided that is compatible with | the Mailgun mail service which will provide reliable deliveries. | */ 'host' => env('MAIL_HOST', 'smtp.gmail.com'), /* |-------------------------------------------------------------------------- | SMTP Host Port |-------------------------------------------------------------------------- | | This is the SMTP port used by your application to deliver e-mails to | users of the application. Like the host we have set this value to | stay compatible with the Mailgun e-mail application by default. | */ 'port' => env('MAIL_PORT', 587), /* |-------------------------------------------------------------------------- | Global "From" Address |-------------------------------------------------------------------------- | | You may wish for all e-mails sent by your application to be sent from | the same address. Here, you may specify a name and address that is | used globally for all e-mails that are sent by your application. | */ 'from' => [ 'address' => env('MAIL_FROM_ADDRESS', 'hicham.braima@gmail.com'), 'name' => env('MAIL_FROM_NAME', 'PMT Admin'), ], /* |-------------------------------------------------------------------------- | E-Mail Encryption Protocol |-------------------------------------------------------------------------- | | Here you may specify the encryption protocol that should be used when | the application send e-mail messages. A sensible default using the | transport layer security protocol should provide great security. | */ 'encryption' => env('MAIL_ENCRYPTION', 'tls'), /* |-------------------------------------------------------------------------- | SMTP Server Username |-------------------------------------------------------------------------- | | If your SMTP server requires a username for authentication, you should | set it here. This will get used to authenticate with your server on | connection. You may also set the "password" value below this one. | */ 'username' => env('MAIL_USERNAME'), 'password' => env('MAIL_PASSWORD'), /* |-------------------------------------------------------------------------- | Sendmail System Path |-------------------------------------------------------------------------- | | When using the "sendmail" driver to send e-mails, we will need to know | the path to where Sendmail lives on this server. A default path has | been provided here, which will work well on most of your systems. | */ 'sendmail' => '/usr/sbin/sendmail -bs', /* |-------------------------------------------------------------------------- | Markdown Mail Settings |-------------------------------------------------------------------------- | | If you are using Markdown based email rendering, you may configure your | theme and component paths here, allowing you to customize the design | of the emails. Or, you may simply stick with the Laravel defaults! | */ 'markdown' => [ 'theme' => 'default', 'paths' => [ resource_path('views/vendor/mail'), ], ], /* |-------------------------------------------------------------------------- | Log Channel |-------------------------------------------------------------------------- | | If you are using the "log" driver, you may specify the logging channel | if you prefer to keep mail messages separate from other log entries | for simpler reading. Otherwise, the default channel will be used. | */ 'log_channel' => env('MAIL_LOG_CHANNEL'), ]; 在内部调用的distance函数中正在引发错误。这使我相信,当函数要使用标量数据时,您正在传递Series对象。

请参见Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()中的讨论,其中在pandas Series上使用python逻辑运算符会导致相同的错误。

对于您而言,解决方案是遍历数据,然后一次将每一组坐标传递给您的函数。

is_proximal