[Python] Series won't convert tolist()

Discussão em 'Python' iniciado por Stack, Fevereiro 13, 2025.

  1. Stack

    Stack Membro Participativo

    Data Object:

    "FiveGigabitEthernet1/0/1": {
    "enabled": true,
    "data": "10",
    "voice": "20",
    "mode": "None",
    "serviceOutput": "None",
    "serviceInput": "None",
    "noMacro": "None",
    "template": "None",
    "tracking": "None",
    "speed": "None",
    "duplex": "None",
    "nonnegotiate": "None",
    "trusted": "None",
    "channelGroup": "None",
    "channelProtocol": "None",
    "ipAddress": "None",
    "ipMask": "None",
    "nativeVlan": "None",
    "allowedVlan": "None"
    },
    "TenGigabitEthernet1/1/1": {
    "serviceOutput": "POLICY",
    "channelGroup": "1",
    "channelProtocol": "active",
    "nonnegotiate": true,
    "mode": "trunk",
    "allowedVlan": ["2", "10", "20", "30"],
    "nativeVlan": "2",
    "enabled": true,
    "data": "None",
    "voice": "None",
    "serviceInput": "None",
    "noMacro": "None",
    "template": "None",
    "tracking": "None",
    "speed": "None",
    "duplex": "None",
    "trusted": "None",
    "ipAddress": "None",
    "ipMask": "None"
    }


    Pandas Init:

    unique = r"GigabitEthernet0\/0|Bluetooth|AppGigabitEthernet|TwentyFiveGigE|FortyGigabitEthernet|Vlan"
    ints = pd.DataFrame.from_dict(dataResult, orient="index")
    ints.index.name = "port"
    ints = ints.reset_index()
    test = ints[(ints["mode"] != "trunk") & (~ints["port"].str.contains(unique,case=False,na=False))]
    print(test)


    The above output prints a correctly filtered database with the ports I want. Test also shows a data type as "series". Why can't I use the ".tolist()" method to get a return as a python list as the port's column?

    Picture of test data type.

    The below errors out with a type error because python thinks test is a dataframe instead of a series.

    test = ints[(ints["mode"] != "trunk") & (~ints["port"].str.contains(unique,case=False,na=False))].tolist()
    print(test)


    Picture of type error.

    Continue reading...

Compartilhe esta Página