How to update value in column cells based on other column cells in the same csv file bys using python?

tierrytestu

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.

DragonBobZ

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Replacing cells in a column, but not header, in a csv file with python

From Dev

Adding multiple cells in a column based on a value in another column but same rows

From Dev

Count cells based on a comparison with value in the same row of another column

From Dev

How to disable (make read only) a cell in a DataGridView CheckBox column based on the value in other cells?

From Java

How to replicate same values based on the index value of other column in python

From Dev

Fill empty cells in column with value of other columns

From Dev

How to search for empty cells in a specific column in a CSV file for all rows?

From Dev

How to merge cells in same column, apply rowspan?

From Dev

how to write an empty column in a csv based on other columns in the same csv file

From Dev

Update a column based on the value of other column

From Dev

Intersect the cells value in column with cells value in row?

From Dev

Merging cells within a column if the same data is in the cells

From Dev

Excel: Using a calculated average from a column in cells in that same column

From Dev

How to split merged cells in a CSV file using python

From Dev

python update a column value of a csv file according to another csv file

From Dev

Excel: Conditionally Select Range of Cells Based on Values in Other Column

From Dev

How do I update a column based on other columns in the same row

From Dev

VBA code to copy cells in specific column and paste (value) it to cells in specific column on another Sheet based on conditions

From Dev

How to use apps script to count cells number and change the context in other column based on it?

From Dev

How to use apps script to count cells number and change the context in other column based on it?

From Dev

Counting empty cells on a column, where other column have filled cells

From Dev

Counting empty cells on a column, where other column have filled cells

From Dev

How to fill empty cells in a CSV with data from a previous cell in the same column?

From Dev

Function to assign cell value to subsequent NA-cells (same column)

From Dev

Powershell to count cells in one column based on filter value

From Dev

Color coding cells based on value through a column with specific header

From Dev

openpyxl color cells based on value from another column

From Dev

Update a row if a column value is not the same on other table

From Dev

Pandas fill cells in a column with NaN values, derive the value from other cells in the row

Related Related

  1. 1

    Replacing cells in a column, but not header, in a csv file with python

  2. 2

    Adding multiple cells in a column based on a value in another column but same rows

  3. 3

    Count cells based on a comparison with value in the same row of another column

  4. 4

    How to disable (make read only) a cell in a DataGridView CheckBox column based on the value in other cells?

  5. 5

    How to replicate same values based on the index value of other column in python

  6. 6

    Fill empty cells in column with value of other columns

  7. 7

    How to search for empty cells in a specific column in a CSV file for all rows?

  8. 8

    How to merge cells in same column, apply rowspan?

  9. 9

    how to write an empty column in a csv based on other columns in the same csv file

  10. 10

    Update a column based on the value of other column

  11. 11

    Intersect the cells value in column with cells value in row?

  12. 12

    Merging cells within a column if the same data is in the cells

  13. 13

    Excel: Using a calculated average from a column in cells in that same column

  14. 14

    How to split merged cells in a CSV file using python

  15. 15

    python update a column value of a csv file according to another csv file

  16. 16

    Excel: Conditionally Select Range of Cells Based on Values in Other Column

  17. 17

    How do I update a column based on other columns in the same row

  18. 18

    VBA code to copy cells in specific column and paste (value) it to cells in specific column on another Sheet based on conditions

  19. 19

    How to use apps script to count cells number and change the context in other column based on it?

  20. 20

    How to use apps script to count cells number and change the context in other column based on it?

  21. 21

    Counting empty cells on a column, where other column have filled cells

  22. 22

    Counting empty cells on a column, where other column have filled cells

  23. 23

    How to fill empty cells in a CSV with data from a previous cell in the same column?

  24. 24

    Function to assign cell value to subsequent NA-cells (same column)

  25. 25

    Powershell to count cells in one column based on filter value

  26. 26

    Color coding cells based on value through a column with specific header

  27. 27

    openpyxl color cells based on value from another column

  28. 28

    Update a row if a column value is not the same on other table

  29. 29

    Pandas fill cells in a column with NaN values, derive the value from other cells in the row

HotTag

Archive