简体   繁体   中英

Python - Pandas Insert Column in Dataframe Once

I'm writing a Python program to lookup the MX record of a list of domains and spit it out to an CSV file. So far everything is working but I'm running into one small issue I can't seem to get across.

Here is my code:

import dns.resolver
import pandas as pd

mx = []
domans = []
provider = pd.Series([])
domain = input("Domain Name\n")
result = dns.resolver.resolve(domain, 'MX')
for exdata in result:
  exdata = str(exdata)
  exdata = exdata.split(" ")
  mx.append(exdata[1])

domain = domain + ","
num_domains = domain * len(mx)
num_domains = str(num_domains)
num_domains = num_domains.split(",")

for g in num_domains:
  g = str(g)
  g = g.split(",")
  domans.append(g[0])

if any("mail.protection.outlook.com" in x for x in exdata):
  provider = "Office 365"
elif any("google" in x for x in exdata):
  provider = "Google"
elif any("mimecast" in x for x in exdata):
  provider = "Mimecast"

dataset = pd.DataFrame(list(zip(domans, mx)))
dataset.insert(2, "Provider", provider) 
dataset.sample(0)
dataset.columns = ['domain','mx record','Provider']
dataset.dropna(axis = 0, how = 'any', inplace = True)
dataset.index = pd.RangeIndex(len(dataset.index))

dataset.to_csv (r'export_dataframe.csv', index = False, header=True)

Here is the CSV result when the input is google.com

domain,mx record,Provider
google.com,alt3.aspmx.l.google.com.,Google
google.com,alt1.aspmx.l.google.com.,Google
google.com,alt2.aspmx.l.google.com.,Google
google.com,alt4.aspmx.l.google.com.,Google
google.com,aspmx.l.google.com.,Google

All I want is for the Provider to only print "google" one time. This way I can look at the CSV list and capture an accurate number of providers.

Any input is appreciated.

I'm not sure what output you are expecting, but it is possible using Groupby to make it a single line while keeping the MX records.

df = df.groupby(['Provider','domain'])['mx record'].agg(list)
df.to_csv (r'export_dataframe.csv', index=True, header=True)

export_dataframe.csv

Provider,domain,mx record
Google,google.com,"['alt3.aspmx.l.google.com.', 'alt1.aspmx.l.google.com.', 'alt2.aspmx.l.google.com.', 'alt4.aspmx.l.google.com.', 'aspmx.l.google.com.']"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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