pandas create a new column based on condition of two columns

Solutions on MaxInterview for pandas create a new column based on condition of two columns by the best coders in the world

showing results for - "pandas create a new column based on condition of two columns"
Emilia
26 Feb 2019
1conditions = [
2    df['gender'].eq('male') & df['pet1'].eq(df['pet2']),
3    df['gender'].eq('female') & df['pet1'].isin(['cat', 'dog'])
4]
5
6choices = [5,5]
7
8df['points'] = np.select(conditions, choices, default=0)
9
10print(df)
11     gender      pet1      pet2  points
120      male       dog       dog       5
131      male       cat       cat       5
142      male       dog       cat       0
153    female       cat  squirrel       5
164    female       dog       dog       5
175    female  squirrel       cat       0
186  squirrel       dog       cat       0
19
queries leading to this page
pandas create new column based on other columns if elsepandas np select multiple conditionsadd new column based on condition on some other column in pandas pandas calculate new column based on old column if elsecheck two column condition dynamically pandascreate a new column based on condition in two dataframenp where multiple conditions multiple resultscondition where 2 columns pandashow to combine pandas columns for conditionpandas combining two column with if statementscreate multiple columns in pandas based on conditionshow to create a new column by adding two other columns pandasadd column from one dataframe to another based on condition pandas add multiple columns based on conditionpandas two columns conditioncreate new column in pandas based on two columnscreate dataframe with 2 columns pythoncombine 1 column dataframe with another 1 cooumn dataframe pandashow to make add a column to a dataframe in python conditionalcreate column using two other columns python dataframepandas set value of column either of two columns based on conditionpandas add column based on calculation from two other columnspandas new column from two columnscreate two new columns in pandas dataframecreate a dataframe with two columns valuespandas new column if two columns are equalapply if statement in a column of datasetpandas create column based on conditionappend columns dataframe based on conditionpandas create dataframe with one column and two rowscreate a new integer column based on multiple condition pandaspandas create new columns conditionpandas select columns by conditiondataframe create new column based on condition for some rowsusing multiple conditions in np wherecreate 2 column dataframe pandascreate new column in pandas dataframe based on multiple conditiondataframe create new columns based on two columnspandas assign one column or the other depending on conditioncreate new column from two columns pandascreate column given condition pandashow to add a new column in pandas dataframe by applying some condition on different columnapply condition on two columns pandasin df if three columns data apply my condition create new columncompare 2 dataframe and add new column pandaspandas new column based on two columnshow to condition 2 columns in pandascreate new column ased in two other columns pandasadd a columns in pandas with if else condition from different columnshow to make conditions with 2 columns in pandaspython dataframe new column based on other columns with different rowhow do i add a new colnum in dataframe based on the condition of some other colnumpandas create a column based on another conditionand two columns pandasmake new column based on condition pandasapplying two conditions pandas dataframeif old column is between interval then create new column in pythonpandas combine 2 column values as new columcreate a new column in pandas based on conditioncompare 2 columns and add new column pandasadd new column to dataframe based on conditioncreate new column with multiple conditions in pandas pythonset value of column dataframe based on two other columnsif column contains two values then create new column with value python dfcondition based on multiple columns pandaspandas create many new columns based on conditionnp select multiple conditionsmerge two columns in pandas dataframe based on conditionpd conditional two columnspandas create column on conditionselect condition 2 column dataframeadd new column with condition pandas dataframebased on condition create column in pandas create new column based on condition pandas fastest waypandas create a new column based on comparison of two other columnscreate a column in python based on condition on other columnpandas create column based on condition from another columnpandas create a column based on another column multiple conditionscreate pandas field into a if conditioncreate a new column based on condition in pandaspandas create a new column based on conditioncreate new column based on multiple condition pandasnp select multiple conditions pandaspandas create 3 new column based on conditioncombine two dataframes pandas based on columnhow to create a new dataframe with if statementadding a new column in pandas based on condition of multiple columnspandas merge two columns on conditiondataframe compare two columns to add a new columnpandas add column to dataframe based on conditionpandas if pandas new column from two columns when at least one of them has valuecombine different columns from two dataframes pandas give preference to columns in one dfhow to merge two dataframes in pandas based on one column valuepython create new column based on conditionpandas dataframe compare two columns and insert value to new columnselect data based on condition in two columns pandaspandas new column conditional on other columnspandas new column based on multiple conditionscreate a df with one columncreate dataframe with 2 columnspandas add conditional column based on two other columnscombine dataframes based on columncreate dataframe with single columnif else between two pandas dataframe add third columnnp where with multiple conditionshow to create a column of a dataframe based on a conditionpandas create 3 new column based other row conditionpandas create a new column by adding 2 other columnshow to make condition in new column df in pandaspandas create column based on two columnscreate dataframe with existing columns of two dataframesnumpy select multiple conditionspython pandas create column based on conditionhow to flag 0 or 1 if some condition is met in python dataframecreate new column in pandas based on conditionif statement across multiple columnspandas create column based on conditions of other columnsadd new column in dataframe based on condition from anotherpandas if condition on column and create new columnpandas set column to another column based on conditionpandas set column based on two column comarisonpandas combine two dataframes based on columnmultiple condition in np selectpandas create new column with multiple if statementnp select multiple conditionscreate several columns based on condition pandaspandas create new column based on other columns row beforenp select with multiple conditionscreate column with if statement pythonmultiple column condition pandascreate a column based on a conditional in pandashow to create single columns as a datframe in pythonpandas create a new column by combining two columnspandas merge rows based on conditioncreate a column based on condition and another dataframe pandascreate a new new column based on multiple condition pandaspandas create column based on multiple conditionscreate column based on condition on two other columns pandashow to add a data to a dataset in python in if elsecompare two dataframe and add new columnsset new column based on conditional pandaspandas create new column based on multiple condition if elsecreate multiple column based on condition of another column pandascreate new column in pandas dataframe based on conditionpandas add column based on condition of other columnspandas create new column based on multiple conditionsif condition on multiple columns in pandaspandas create a column based on conditionpandas apply condition of two columns create a new dataframe with 2 columnspandas apply two columnscreate new column based on two columns pandaspandas add 2 columns based on other columnscompare two dataframe columns and insert value into another columnadd two column conditions pandaspandas create new column based on other columnspandas create new column based on multiple conditionpandas create dataframe from two columns if they are differentcreate new column pandas based on conditionhow to make a dataframe pandas from two existing columnspandas dataframe create a new column from calculation of two other columns for each rowselect dataframe rows based on multiple conditions on columns pythonhow to make a new dataframe with same columnshow to create new column in pandas dataframe based on conditionadd value new column to dataframe based on conditionpython new column if two columns are truecreate a column based on two column pandascreate two new columns panda dataframecreate dataframe with existing columns of two dataframes inepandas create new column conditional on other columnscreate new dataframe based on multiple column valuepython add new column condition on 2 columns rowspython pandas new column based on two condition columns conditionapply if condition on 2 columns in pandascreate pandas column with new values based on values in other columnscreate a new column in pandas by a condition of anotherapply if pandas two columnscombine values of two columns in pandaspandas create two new columns from applyif two columns are equal then add two other columns pandascreate new column in python based on conditionpandas create new column based on conditionpandas add column based on two other columnsadd new column in dataframe based on condition from another dataframecreate new column from calculation on 2 other columns in pandaspandas new column based on another column conditionpandas create a column from 2 different columns based on conditionpandas dataframe set column value based on other two columnshow do i make one column equal another column in two pandas dataframeif else between two pandas dataframe add thord columncreate new column based on condition pandashow to create a new column in pandas based on a conditionadd a column in pandas based on conditionpandas create new column based on two other columnspandas assign value to multiple column based on conditionadd a column to a pandas dataframe based on an if else conditionpandas dataframe create column based on conditionadding a new column in pandas based on conditions of multiple columnsconcatinate 2 column to one pandas with conditionhow to create a new column based on multiple condition pandaspandas create a new column based on condition of two columns