Skip to content
Advertisement

Panda iterating through CSV matching index out-of-bounds

I’m running into an issue with my locate / match statement. I’m trying to match the column “IP” assign it as the index and iterate through a csv of a few thousand hosts.

Whenever the below script makes its way back up to process the second ‘IP’ I fail with a “single positional indexer is out-of-bounds”.

Thank you all.

def whoisyou(df):
    s = socket.socket()
    s.settimeout(10)
    for index, row in df.iterrows():
        DN = df.iloc[index]["ip"]
        ipwhois = IPWhois(DN).lookup_rdap(asn_methods=["dns", "whois", "http"])
        network = ipwhois["asn_cidr"]
        cidr = ipwhois["asn_description"]
        country = ipwhois["asn_country_code"]
        date = ipwhois["asn_date"]
        df = pd.DataFrame(columns=[DN, cidr, country, date, network])
        whoareyou = df.to_csv("output.csv", index=False, mode="a")
        time.sleep(5)

Here is the csv content:

https://i.stack.imgur.com/EziH4.png

Advertisement

Answer

It seems you are not using iterrows properly and there are several inconsistencies in your code.

Try to define your whoisyou function like this:

def whoisyou(df):
    for _, row in df.iterrows():
        DN = row["ip"]
        try:
            ipwhois = IPWhois(DN).lookup_rdap(asn_methods=["dns", "whois", "http"])
            row["network"] = ipwhois["asn_cidr"]
            row["cidr"] = ipwhois["asn_description"]
            row["country"] = ipwhois["asn_country_code"]
            row["date"] = ipwhois["asn_date"]
        except KeyError:
            continue
        time.sleep(5)

    df.to_csv("output.csv", index=False, mode="a")

And then you could call it like this:

import pandas as pd

df = pd.read_csv("your_hosts_file.csv")

# Check source data
print(df)

                ip cidr country date network
0  146.112.255.155
1    168.149.132.2
2  168.149.132.176
3     104.40.69.64
4   152.195.12.135
5    34.120.111.39

# Get infos, fulfill the df and save it as a csv file
whoisyou(df)
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement