Home > database >  Combining functions for data frame columns inside for loop
Combining functions for data frame columns inside for loop

Time:03-27

How to combine it to single function and append all values to single df

My trials were create df_network inside network function and then create df_memory inside memory function, and then try concatenate two differnt df.

This works

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()

Something like this but i get error for below trial - If i try in single function

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             |

CodePudding user response:

The simplest solution here would be to use pd.concat with axis=1 to combine the two dataframes:

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()

CodePudding user response:

I think this will do what you're asking:

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())

Output:

  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

CodePudding user response:

You can use 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)

Output:

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