0

Let's suppose that I have a big data in a csv file:

frame.number    frame.len   frame.cap_len   frame.Type  
  1               100           100           ICMP  
  2                64            64           UDP   
  3               100           100           ICMP  
  4                87            64           ICMP  

I want to change the type of the frame based on its length. The first problem is that I don't know how to extract the rank of the column, then change the frame typelike this:

if frame.len==100 then it puts frame.type=ICMP_tt else if frame.len==87 then it puts frame.type=ICMP_nn 

I would like that it looks like this:

frame.number    frame.len   frame.cap_len   frame.Type  
  1               100           100           ICMP_tt   
  2                64            64           UDP   
  3               100           100           ICMP_tt   
  4                87            64           ICMP_nn

I try by using this code but it doesn't make any modification.

import pandas

df = pandas.read_csv('Test.csv')
if df['frame.len'] == 100:
    df['frame.type'].replace("ICMP_tt")

I would be very grateful if you could help me please.

tierrytestu
  • 119
  • 4
  • 12

1 Answers1

0

Similar question: How to conditionally update DataFrame column in Pandas

import pandas
df = pandas.read_csv('Test.csv')
df.loc[df['frame.len'] == 100, 'frame.Type'] = "ICMP_tt"
df.loc[df['frame.len'] == 87, 'frame.Type'] = "ICMP_nn"
df

Result: enter image description here

Should do the trick. The first item given to df.loc[] is an array full of True/False values telling it which rows to update (will also accept single number as the row index if I recall correctly), and the second item specifies which column to update.

DragonBobZ
  • 2,194
  • 18
  • 31
  • But I don't have any modification, that it is my problem. – tierrytestu Oct 21 '17 at 19:47
  • Do you mind sending me a csv or a pickle? Pickle would be better. By send I mean something like sharing it on google drive... – DragonBobZ Oct 21 '17 at 20:00
  • it is a big file, I try to test with those set of lines. Do you think that I need to save results in a new files. – tierrytestu Oct 21 '17 at 20:04
  • Yes, it will not directly affect the csv file; it will only alter the data in the DataFrame object. For it to actually affect your csv you will have to write it out using something like `df.to_csv('output.csv', index=False)`. – DragonBobZ Oct 21 '17 at 20:08