user11509642
user11509642

Reputation:

Update Dataframe column value based on other Dataframe column value

I have a pandas Dataframe which has a couple of columns. I want to get the first 3 elements out of the Information Column based on a value in Protocol.

For example: I want the first 3 elements in Information IF the protocol is TCP.

Using below code I can separate the columns needed for my operation. But I have no clue how to adapt the next piece of code to this.

chunk[['Protocol', 'Information']] = chunk[['Protocol', 'Information']]

EDIT:

I wish to update the values. Not separate them.

Upvotes: 1

Views: 524

Answers (2)

danialmalik
danialmalik

Reputation: 61

You can use something like this:

import pandas

data = data = {'Name':['first', 'second', 'third', 'fourth'],
        'Age':[27, 27, 22, 32],
        'Address':['New York', 'ABC', 'XYZ', 'Nowhere'],
        'Qualification':['Msc', 'MA', 'MA', 'Phd']}

# Make a dataframe object
df = pandas.DataFrame(data)

# Your condition
# for example we want to get the rows with `Qualitication=='MA'
is_MA_qualified = df['Qualification'] == 'MA'

# Now to filter your data
MA_qualified = df[is_MA_qualified]

# You can use `head(n)` to get first three rows
first_three_MA_qualified = MA_qualified.head(3)

# And finally, to get any desired columns
first_three_MA_qualified[['Age','Address']]

UPDATE: to update cells, you can iterate over rows and then change the values of the cells that fulfill the condition:

...
for index, row in df.iterrows():
    if row['Age'] >= 18:
        df.at[index, 'Qualification'] = 'Verified'

Upvotes: 1

user11509642
user11509642

Reputation:

I have been able to update the values based on a given value:

chunk.loc[chunk['Protocol'] == tcp, 'Information'] = 5

But right now I just change the value to the number 5. I would rather use a lambda expression or function to get the first three elements and only keep these values.

Upvotes: 1

Related Questions