简体   繁体   English

将 pandas 数据帧/系列应用于 netaddr EUI 构造函数

[英]Apply pandas dataframe/series to netaddr EUI constructor

I have a dataframe with Mac addresses, which needs converting.我有一个带有 Mac 地址的数据框,需要转换。 I first do the following to clear out any nan 's:我首先执行以下操作以清除任何nan

df = df[pd.notnull(df["MacAddr"])]["MacAddr"] # avoid null values being passed to netaddr

...then I'd like to parse into the EUI constructor like so: ...然后我想像这样解析到EUI构造函数中:

from netaddr import EUI, mac_bare

mac = EUI(df)
mac.dialect = mac_bare

But this doesn't work, because EUI expects a string, not a pandas Series.但这不起作用,因为 EUI 需要一个字符串,而不是一个熊猫系列。 Is there a way to circumvent this?有没有办法绕过这个?

As an error I get this:作为一个错误,我得到了这个:

Name: MacAddr, Length: 26953, dtype: object is not str() or unicode()!

从系列中创建一个列表,然后将元素连接成一个字符串。

 ','.join(df[pd.notnull(df["MacAddr"])]["MacAddr"].to_list())

Since parsing dataframes/series isn't currently possible for the EUI constructor, I created a function for df.apply() , like so:由于 EUI 构造函数目前无法解析数据帧/系列,因此我为df.apply()创建了一个函数,如下所示:

def main(self):
    def _eui_mac_bare_func(x):
        mac = EUI(x)
        mac.dialect = mac_bare
        return mac

series = df[pd.notnull(df["MacAddr"])]["MacAddr"] # avoid null values being passed to netaddr
new_series = series.apply(_eui_mac_bare_func) # apply function to every row
df = df.join(new_series.rename("Norm_MacAddr")) # join df and series on corresponding indices

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

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