last day (16 days later) » 

7:20 AM
0
A: How to convert all IP address lists to decimal numbers in Pandas

anky_91IIUC, Considering the dataframe name of the output is df, something like this: import socket, struct def ip2int(ip): """ Convert an IP string to int """ packedIP = socket.inet_aton(ip) return struct.unpack("!L", packedIP)[0] df['ip_int'] = df.Ip.apply(ip2int) df['range_sum...

 
use this function under this line? k = pdd [pdd ['Ipv'] .str.contains ("ipv4") & pdd ['Ip']. str.contains ('[0 - 9]')]
 
@warezers this is after you have the output. df = k[['Ip','Range','Code']]
 
got some error: Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: pandas.pydata.org/pandas-docs/stable/… k['range_sum']=k.groupby(['Code'])['Range'].transform('sum')
 
@warezers that is because k is a slice try assigning k to a dataframe , df = k[['Ip','Range','Code']] , also before that execute this line pd.options.mode.chained_assignment = None after that use the code. :)
 
Now it's not an error and if I get the output of this value only? # df[df.Code.duplicated(keep='last')]) print(df[['ip_int','range_sum','Code']])
 
7:20 AM
@warezers i didnt get you this time , if you omit the df[df.Code.duplicated(keep='last')]) you will get all the values and not just the first occurance
 
like to get output only this 33554432 79116144 FR
 
oh, yes you can slice whatever column you want to see
:)
 
Can I use this way?
def ip2int(ip):
"""
Convert an IP string to int
"""
packedIP = socket.inet_aton(ip)
return struct.unpack("!L", packedIP)[0]

df['ip_int'] = df.Ip.apply(ip2int)
df['range_sum']=df.groupby(['Code'])['Range'].transform('sum')
df[df.Code.duplicated(keep='last')]
print(df[['ip_int','range_sum','Code']])
This line is get an underline code
df[df.Code.duplicated(keep='last')]
 
df[df['ip_int']==33554432]
for 33554432
and no need for df[df.Code.duplicated(keep='last')]
that line
let me know if that works
This gives you what you wantdf.loc[df['ip_int']==33554432,['Code','range_sum','ip_int']]
df.loc[df['ip_int']==33554432,['Code','range_sum','ip_int']]
you can rearrange the columns under loc as you want the order to look like. :)
If you are getting the values as intended, please consider upvoting+accepting my answer, thanks in advance. :)
 
Oh I understand if I want to see only that 33554432
df.loc [df ['ip _ int'] == 33554432, ['Code', 'range_sum', 'ip_int']]
If I want everything, I need this
df [df.Code.duplicated (keep = 'last')]
 
7:31 AM
no
if you want everything just use df[['Code', 'range_sum', 'ip_int']]
if you just want the first occurance per country
then use the duplicated line
else leave that
 
Ya I got it
Thank you for the explanation method easy to understand
 
Pleasure..!! :)
Thanks for accepting
 
Sorry for the last question
Can I read more than one file to get output like this?
cuz I am only using one file read
filename = "/Users/sda/Desktop/file"
pdd = pd.read_csv(filename,header=None,sep='|',error_bad_lines=False, warn_bad_lines=False,skiprows=[0],
names=['Name','Code','Ipv', 'Ip','Range','Date', 'Category'])
 
umm, to read more than one file in the folder use glob, you will find a lot of example in SO, just search one. :) but that will involve loops and may be you have to append the Dataframes generated in loops, again this is available in SO. :)
incase you get stuck
post a fresh question and you will definately get an answer.
 
oh thank you
 

  last day (16 days later) »