Home > Enterprise >  Pandas - modify cell if match and if not match delete row
Pandas - modify cell if match and if not match delete row

Time:10-22

i need modify a csv file with pandas, I have the following table:

Interface       Description
Gi0/3/4/3       ISP
Gi0/3/4/3.401   Cliente A
Gi0/3/4/3.402   Cliente B
Gi0/3/4/3.403   Cliente C
Gi0/3/4/4       Cliente D
Gi0/3/4/4.1     Cliente E
Gi0/3/4/4.2     Cliente F
Gi0/3/4/5       Cliente G

I need to modify the Interface column, if the cell matches Gi0/3/4/3. I need to remove this part of the string and leave the rest and if it doesn't match Gi0/3/4/3. delete the row and finally make it look like this:

Interface       Description

401         Cliente A
402         Cliente B
403         Cliente C

From already thank you very much!

CodePudding user response:

Extract the suffix after Gi0/3/4/3. and then filter by non nulls:

df.Interface = df.Interface.str.extract('Gi0/3/4/3\.(\d )')
df[df.Interface.notnull()]

  Interface Description
1       401   Cliente A
2       402   Cliente B
3       403   Cliente C
  • Related