在 for 循环中组合数据框列的函数

Combining functions for data frame columns inside for loop

如何将其合并为单个函数并将所有值附加到单个 df

我的试验是创建 df_network 内部网络函数,然后创建 df_memory 内部内存函数,然后尝试连接两个不同的 df。

这个有效

def network():
    df_network = pd.DataFrame(
        ([i.DNSHostName, i.ipaddress] for i in conn.Win_ddd()),
        columns=['DNSHostName', 'ipaddress']
    )
    return df

def memory():
    df_memory = pd.DataFrame(
        ([i.Caption, i.TotalPhysicalMemory] for i in conn.Win_sss()),
        columns=['Caption', 'TotalPhysicalMemory']
    )
    return df
df_network = network()
df_memory = memory()

类似这样,但我在下面的试用中遇到错误 - 如果我尝试使用单一功能

def total():
    df = pd.DataFrame(
        ([i.DNSHostName, i.ipaddress] for i in conn.Win_ddd()),
        columns=['DNSHostName', 'ipaddress']
        ([i.Caption, i.TotalPhysicalMemory] for i in conn.win_sss()),
        columns=['Caption', 'TotalPhysicalMemory']
    )
    return df

df.head()

| DNSHostName | ipaddress    | Caption | TotalPhysicalMemory |
|-------------|--------------|---------|---------------------|
| AAA         | xx.xx.xxx.xx | RRR     | 3434334             |
| BBB         | xx.xx.xxx.aa | FFF     | 6456456             |

我认为这会满足您的要求:

class Ddd:
    def __init__(self, DNSHostName, ipaddress):
        self.DNSHostName = DNSHostName
        self.ipaddress = ipaddress
class Sss:
    def __init__(self, Caption, TotalPhysicalMemory):
        self.Caption = Caption
        self.TotalPhysicalMemory = TotalPhysicalMemory

ddd = [Ddd('host' + str(i), '000.00.0000') for i in range(5)]
sss = [Sss('caption' + str(i), 100000000000) for i in range(5)]

def total():
    df = pd.DataFrame(
        ([i.DNSHostName, i.ipaddress] for i in ddd),
        columns=['DNSHostName', 'ipaddress']
    )
    df[['Caption', 'TotalPhysicalMemory']] = pd.DataFrame([i.Caption, i.TotalPhysicalMemory] for i in sss)
    return df

print(total())

输出:

  DNSHostName    ipaddress   Caption  TotalPhysicalMemory
0       host0  000.00.0000  caption0         100000000000
1       host1  000.00.0000  caption1         100000000000
2       host2  000.00.0000  caption2         100000000000
3       host3  000.00.0000  caption3         100000000000
4       host4  000.00.0000  caption4         100000000000

这里最简单的解决方案是使用 pd.concataxis=1 来组合两个数据帧:

def total():
    df = pd.concat([
        pd.DataFrame(
            ([i.DNSHostName, i.ipaddress] for i in conn.Win_ddd()),
            columns=['DNSHostName', 'ipaddress']
        ),
        pd.DataFrame(
            ([i.Caption, i.TotalPhysicalMemory] for i in conn.Win_sss()),
            columns=['Caption', 'TotalPhysicalMemory']
        )
    ], axis=1)
    return df

df_total = total()

您可以使用 chain.from_iterable:

from itertools import chain

d1 = [[i.DNSHostName, i.ipaddress] for i in conn.Win_ddd()]
d2 = [[i.Caption, i.TotalPhysicalMemory] for i in conn.Win_sss()]

cols = ['DNSHostName', 'ipaddress', 'Caption', 'TotalPhysicalMemory']
df = pd.DataFrame((chain.from_iterable(i) for i in zip(d1, d2)), columns=cols)

输出:

>>> df
  DNSHostName ipaddress Caption  TotalPhysicalMemory
0         AAA   a.b.c.d     RRR              3434334
1         BBB   e.f.g.h     FFF              6456456