0
import pandas as pd

df = pd.DataFrame([[1,'192.168.1.10', '45.7.12.34', 'abc'],[4, '10.10.1.11', '90.90.67.33', 'def'], [77, '52.1.7.90', '67.5.3.5', 'ghi' ], [90, '19.19.90.7', '77.88.99.44', 'xyz']], columns=['A', 'sip', 'dip', 'location'])
# add new columns
df = df.assign(FromHost='from.noname', ToHost='to.noname')

addrs = {'192.168.1.10': 'myhost.net', '45.7.12.34': 'myhost-goo.net', '10.10.1.11': 'myhost-foo.net' }`enter code here`

So it produces the following table:

>>> df
    A           sip          dip location     FromHost     ToHost
0   1  192.168.1.10   45.7.12.34      abc  from.noname  to.noname
1   4    10.10.1.11  90.90.67.33      def  from.noname  to.noname
2  77     52.1.7.90     67.5.3.5      ghi  from.noname  to.noname
3  90    19.19.90.7  77.88.99.44      xyz  from.noname  to.noname

Now, I need to replace values in 'FromHost' and 'ToHost' columns with addrs dictionary, where key matches 'sip'/'dip' columns respectively. Example:

    A           sip          dip location     FromHost     ToHost
0   1  192.168.1.10   45.7.12.34      abc   myhost.net  myhost-goo.net

I tried e.g. df.loc but it returns DataFrame, I think I need Series. How can I achieve this?

Erfan
  • 40,971
  • 8
  • 66
  • 78
Mark
  • 6,052
  • 8
  • 61
  • 129

0 Answers0