Home > OS >  delete a sub-string for all values in a column pandas
delete a sub-string for all values in a column pandas

Time:03-17

I have this dataframe :

action,adresse_ip,ip_source,ip_dest,client

block,ip="128.30.03.29", source="29E9t9994" ,destination="12300rtgR30" ,client1

block,ip="13.19.04.22",source="29E9th9994" ,destination="12300Rg30" ,client1

allow,ip="40.77.05.293", source="29dfbfE99994", destination="12d300R30" ,client2

block,ip="128.90.73.294", source="29E99fv994" , destination="1230s0R30" ,client2

allow,ip="128.30.83.295", source="29Egfn99994" ,destination="12h300R30",client3

allow,ip="90.03.93.296", source="29E99fv994" ,destination="123k00R30",client5

allow,ip="128.30.04.297", source="29E99994" ,destination="12300tR30" ,client7

block,ip="128.40.01.298", source="29E99994" ,destination="1230z0R30",client8

block,ip="128.50.02.299", source="29E99994" ,destination="1230v0R30" ,client9

I want to delete the last byte for column "adresse_ip", so i want a dataframe like this :

action,adresse_ip,ip_source,ip_dest,client

block,ip="128.30.03", source="29E9t9994" ,destination="12300rtgR30" ,client1

block,ip="13.19.04",source="29E9th9994" ,destination="12300Rg30" ,client1

allow,ip="40.77.05", source="29dfbfE99994", destination="12d300R30" ,client2

block,ip="128.90.73", source="29E99fv994" , destination="1230s0R30" ,client2

allow,ip="128.30.83", source="29Egfn99994" ,destination="12h300R30",client3

allow,ip="90.03.93", source="29E99fv994" ,destination="123k00R30",client5

allow,ip="128.30.04", source="29E99994" ,destination="12300tR30" ,client7

block,ip="128.40.01", source="29E99994" ,destination="1230z0R30",client8

block,ip="128.50.02", source="29E99994" ,destination="1230v0R30" ,client9

Please Help,

Thank you in advance

CodePudding user response:

Use the replace method on the str accessor with a regex.

df['adresse_ip'] = df['adresse_ip'].str.replace('\.\d "$', '"', regex=True)

CodePudding user response:

Or you can use apply function.

df['ip_address'] = df['ip_address'].apply(lambda x: '.'.join(x.split('"')[1].split('.')[:3]))
  • Related