Home > Software design >  How to replace values of first n rows after filtering dataframe?
How to replace values of first n rows after filtering dataframe?

Time:07-05

Suppose we have a dataframe:

import pandas as pd
data = {'text':['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'], 'values':[1, 0, 0, 0, 1, 1, 0, 1, 0, 1]}
df = pd.DataFrame(data)
df

enter image description here

In this DataFrame, how to filter data for values=0, and from filtered data, we want to replace values of text column for the first n rows?

Expected Output:

enter image description here

CodePudding user response:

You can use a mask and boolean indexing:

m = df['values'].eq(1)
df.loc[m.cumsum().le(4)&m, 'text']  = 'o'

Output:

  text  values
0   Ao       1
1    B       0
2    C       0
3    D       0
4   Eo       1
5   Fo       1
6    G       0
7   Ho       1
8    I       0
9    J       1

CodePudding user response:

Solution: Step 1: Filter data for the given condition and get the list of n indices for which we want to update values. In this case, let's assume we are updating values for first 4 rows after filtering data:

filered_index = list(df[df.values==0]['text'].index)[:4] 
filered_index

Output: [1,2,3,6]

Step 2: In the next step, use the filtered_index and select the column text to update the values:

df.loc[filered_index, 'text'] = df.loc[filered_index]['text'].apply(lambda x: x 'o')
df

Output:

enter image description here

Hope this was helpful!!

  • Related