Conditional Replace Pandas

Conditional Replace Pandas

.ix indexer works okay for pandas version prior to 0.20.0, but since pandas 0.20.0, the .ix indexer is deprecated, so you should avoid using it. Instead, you can use .loc or iloc indexers. You can solve this problem by:

mask = df.my_channel > 20000
column_name = 'my_channel'
df.loc[mask, column_name] = 0

Or, in one line,

df.loc[df.my_channel > 20000, 'my_channel'] = 0

mask helps you to select the rows in which df.my_channel > 20000 is True, while df.loc[mask, column_name] = 0 sets the value 0 to the selected rows where maskholds in the column which name is column_name.

Update:
In this case, you should use loc because if you use iloc, you will get a NotImplementedError telling you that iLocation based boolean indexing on an integer type is not available.

Conditional replacement of all values in selected columns in dataframe (pandas)

Depending on how many ValueN columns you have, you can first build a list of them:

cols = [x for x in df.columns if 'Value' in x]

An efficient way is using mask:

df[cols] = df[cols].mask(df[cols] > 0, 1)

Alternatively, you can try np.where:

df[cols] = np.where(df[cols] > 1, 0, df[cols])

Pandas DataFrame: replace all values in a column, based on condition

You need to select that column:

In [41]:
df.loc[df['First Season'] > 1990, 'First Season'] = 1
df

Out[41]:
Team First Season Total Games
0 Dallas Cowboys 1960 894
1 Chicago Bears 1920 1357
2 Green Bay Packers 1921 1339
3 Miami Dolphins 1966 792
4 Baltimore Ravens 1 326
5 San Franciso 49ers 1950 1003

So the syntax here is:

df.loc[<mask>(here mask is generating the labels to index) , <optional column(s)> ]

You can check the docs and also the 10 minutes to pandas which shows the semantics

EDIT

If you want to generate a boolean indicator then you can just use the boolean condition to generate a boolean Series and cast the dtype to int this will convert True and False to 1 and 0 respectively:

In [43]:
df['First Season'] = (df['First Season'] > 1990).astype(int)
df

Out[43]:
Team First Season Total Games
0 Dallas Cowboys 0 894
1 Chicago Bears 0 1357
2 Green Bay Packers 0 1339
3 Miami Dolphins 0 792
4 Baltimore Ravens 1 326
5 San Franciso 49ers 0 1003

Pandas: Conditionally replace values based on other columns values

Now my goal is for each add_rd in the event column, the associated
NaN-value in the environment column should be replaced with a string
RD.

As per @Zero's comment, use pd.DataFrame.loc and Boolean indexing:

df.loc[df['event'].eq('add_rd') & df['environment'].isnull(), 'environment'] = 'RD'

How to conditionally replace values in a pandas dataframe column with values from another column

You missing the 2nd condition and function name should be isna

areas['area3'] = np.where(areas['area1'].isna(), areas['area2'], areas['area1'])

conditional replace in pandas dataframe

You can use mask:

df["B"] = df["B"].mask(df["C"].eq("Japan"), df["B"].str.replace("$", "Y"))

print (df)

A B C
0 121 The price is $4M USA
1 323 The price is $2.2M USA
2 454 The price is Y62K Japan
3 654 The price is Y91M(-21%) Japan
4 877 The price is $432M(91%) USA

Pandas data frame replace values in column based on condition

The problem with your code is, that df is changing to type string during the process.

There exists a pandas function for this usecase, named pd.where().

df = df.where(df['col'].isin(['a', 'b']),'other')

Similar result avoiding where():

df[~df['col'].isin(['a', 'b'])] = 'other'

Replace value in pandas dataframe based on where condition

You can use np.where or Series.mask

df['Feature1'] = df['Feature1'].mask(df['Feature1'].eq(-9999999), df['Age'])
# or
df['Feature1'] = np.where(df['Feature1'].eq(-9999999), df['Age'], df['Feature1'])

How to conditionally replace NaN values in a dataframe?

fillna can take a series to replace NaN values with. Non-NaN values are left untouched.

Replace the month numbers with the values from your dictionary with map, then pass the result to fillna:

df["WL1"] = df.WL1.fillna(df.Month.map(dictionary["WL1"]))


Related Topics



Leave a reply



Submit