pandas where

# replaces values with other where condition is False
DataFrame.where(cond, other=nan, inplace=False, axis=None, level=None, errors='raise', try_cast=False)

import pandas as pd 
df = pd.DataFrame({'values':[1,2,3,4]})

df.where(df['values'] % 2 == 0, -1) # output : [-1, 2, -3, 4]

4
1
Hpib 75 points

                                    In [20]: df
Out[20]:
   A  B  C
0  1  2  1
1  2  3  0
2  3  4  0
3  4  5  1

# Note that df.C is a mask
# Note that this is np.where, not df.where. That is different.
In [21]: df['D'] = np.where(df.C, df.A, df.B)

In [22]: df
Out[22]:
   A  B  C  D
0  1  2  1  1
1  2  3  0  3
2  3  4  0  4
3  4  5  1  4

4 (1 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
pandas where in pd.where() in python .where function in pandas python dataframe where using .where in pandas dataframe.where in python where python dataframe panda. where df where python pandas df.where where df pandas where panda python pandaas where pandas.DataFrame.where¶ dataframe where pandas python panda dataframe where python df where where in python pandas how to use df.where() how to use pd.where() df.where() df.where(df where( ) pandas PANDAS where statement df.where() python pandas .where() where on pandas dataframes Pandas sql where dataframe .where python where pandas .where pd pd.where pandas python where statement dataframe# where in pandas with and .where python pandas pandas dataframe.where pandas pd where panda dataframe where pandas and .where pandas where. where pandas python dataframe where in how to use the where function in pandas does pandas use where datframe pandas where where in dataframe python dataframe pandas where is pd.where where in dataframe how to use where in pandas where in df using where pandas df using where in dataframe python where in python dataframe pandas where method python panda where pd where python pandas dataframe where Df .where pandas where where function pandas pandas where by value where for dataframe in python np where example dataframe pd.df. where dataframe where example .where pandas pandas where clause where in pandas .wehre pandas where in pandas dataframe where in pandas dataframe np.where in pandas df df.where(df in l) dataframe.where example pandas where condition panda where what is pandas df.where dataframe where condition np.where in pandas dataframe df.loc[df.where np.where on pandas dataframe .where in pandas df.where in pandas pandas datagrame.where np where in pandas df.where pandas pandas where in python dataframe.where pandas df find out true how to use where condition in dataframe df.where np.where pandas pnadas .where how to give where condition in pandas dataframe pandas.where python pandas dataframe where df.where (df[X]==10) df.where in python pd where where pandas np.where in pandas dataframe where df where python pandas where pandas pd.where python dataframe np.where pandas where pandas .where
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source