bogdann
bogdann

Reputation: 49

How to extract data the ip address from this format

The rows in column A are always have the following format:

dns_name (dns-server) Ip_addr_of_the_server, datacenter_location. 

An example would be:

linux_test (dns-1-intern) 10.10.10.250, Berlin_DC. 

What argument i should add to split() function to get only the ip address?

The result should be: 10.10.10.250 (just the ip add, that's all)

Upvotes: 0

Views: 1062

Answers (2)

Erfan
Erfan

Reputation: 42916

Method 1:

Using regex with str.extract to extract the string between the ) and , in your column:

# Example dataframe
df = pd.DataFrame({'A':['linux_test (dns-1-intern) 10.10.10.250, Berlin_DC']})

df['IP'] = df['A'].str.extract('(?<=\))(.*?)(?=\,)')

                                                   A             IP
0  linux_test (dns-1-intern) 10.10.10.250, Berlin_DC   10.10.10.250

Method 2:

Using only pandas with str.find and string slicing:

paranthesis = df['A'].str.find(')').values[0]
comma = df['A'].str.find(',').values[0]

df['IP'] = df['A'].str[paranthesis+2:comma]

                                                   A             IP
0  linux_test (dns-1-intern) 10.10.10.250, Berlin_DC   10.10.10.250

Method 3

Using regex again, but this time find the pattern of your IP address

Note, this is not very general, since your IP address can differ

df['A'].str.extract('([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)')

              0
0  10.10.10.250

Upvotes: 1

BENY
BENY

Reputation: 323306

In case you have multiple IP within one string , using findall

df['IP']=df['A'].str.findall(r'(?:\d{1,3}\.)+(?:\d{1,3})').str[0]
df
Out[246]: 
                                                   A            IP
0  linux_test (dns-1-intern) 10.10.10.250, Berlin_DC  10.10.10.250

Upvotes: 1

Related Questions