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.
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
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.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments