add new column based on condition on some other column in pandas

Solutions on MaxInterview for add new column based on condition on some other column in pandas by the best coders in the world

showing results for - "add new column based on condition on some other column in pandas "
Tim
11 May 2016
1# For creating new column with multiple conditions
2conditions = [
3    (df['Base Column 1'] == 'A') & (df['Base Column 2'] == 'B'),
4    (df['Base Column 3'] == 'C')]
5choices = ['Conditional Value 1', 'Conditional Value 2']
6df['New Column'] = np.select(conditions, choices, default='Conditional Value 1')
Oskar
12 Jan 2018
1# np.where(condition, value if condition is true, value if condition is false)
2
3df['hasimage'] = np.where(df['photos']!= '[]', True, False)
4df.head()
Darrin
17 Oct 2018
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
creating pandas dataframe column with conditionallyif statement across multiple columnspython pandas create series based on conditionalpandas two columns conditionpandas add conditional columnpython new column if conditionhow to create a new column by adding two other columns pandaspandas add column with value from another dataframehow to create a new column in pandas based on a conditioncreate new columns based on other column containts pandaspandas create new columns conditionpandas new column based on other columnsapply if condition on 2 columns in pandasif value in one column equals another then pandaspandas set column value conditionally based on other column valuepandas new column based on another column valueadd new column to dataframe based on condition pythonadd column from one dataframe to another based on condition create new column with multiple conditions in pandas pythoncreate new column pandas with conditionpandas new column based on conditionpython create a new column based on conditionpandas dataframe create column based on conditioncreate single column dataframe pandascreate a new column in pandas by a condition of anotherpandas dataframe add column based on conditionpandas create new column based on multiple condition if elsepython pandas dataframe add column based on other columnspython pandas create column based on conditionpandas set column value based on if else conditioncreate column with if statement pythonusing multiple conditions in np wherehow to make a dataframe pandas from two existing columnscreate new column in pandas based on conditionpandas dataframe create column by condition on other columncreate a dataframe column based on other columncreate new column based on another column if pandasadd new column with condition pandasconditionally create column pandasif condition on multiple columns in pandascreate column from other columns pandascompare two dataframe and add new columnspandas add column depending on other columnpandas add value to column based on conditioncreate new column pandas based on other columnspandas create new column based on multiple conditionsnew column pandas based on conditionconvert column conditional pandasand condition in new columns in pandaspandas np select multiple conditionsnp where pandas conditionpandas conditional column based on other columnscreate new column from calculation on 2 other columns in pandaspandas create new columns based on other columnscondition based on multiple columns pandascreate new column based on condition of another column pandasadd two column conditions pandaspandas add a column with same value pandasnew column from function conditional pandascreate column using two other columns python dataframeadding a column in a pd dataframe based on a conditionpython add new column based on other columnswhen pandascreate dataframe column based on condition in another column vectorpandas create column based on two columnscreate new dataframe based on multiple column valuedataframe create new column based on conditionpandas make new column from other columnspandas add column with value based on conditioncompare 2 columns and add new column pandaspandas add column based on conditioncreate a column based on two column pandaspandas create 3 new column based on conditioncreate column pandas with conditionnp where length of column conditionhow to create a new column in pandas based on another columnhow to create a new column based on another column pandasin pandas how to create new column based on the value of another columnpandas set column to another column based on conditioncreate new column from other columns of dataframe how to create a new column in dataframe based on other column valuescreate new column based on multiple condition pandaspandas create new columns based on conditioncreate a new column in pandas based on another column conditional column pandaspandas create new column based on condition if elsecombine values of two columns in pandaspython how to conditionally add items in a new columnadd columns based on another column value dfadding a new column in pandas based on condition of multiple columnspandas add new column based on another columncreate new column with condition pandascreate new column in dataframe pandas based on conditioncreate column based on another column pandaspandas create column based on other columnscombine different columns from two dataframes pandas give preference to columns in one dfadd column to pandas dataframe with value based on other columnadd column to dataframe based on other columns valuepython dataframe if else in one linehow do i make one column equal another column in two pandas dataframeif else between two pandas dataframe add third columnpandas create new column conditional on another columnnp select multiple conditionsadd dataframe column based on other columns pandaspandas if else one linehow to create a new pandas column based on condition from another columnset new column based on conditional pandaspandas new column value based on other columnscombine dataframes based on columnpandas new column if two columns are equalpandas create a column based on conditionpandas add columns based on other columnswhile adding one column to other dataframe column value pandaspandas series calculate conditional selectpandas if column value 3e than other columncompare two dataframe columns and insert value into another columncreate a new new column based on multiple condition pandaspandas if condition on column and create new columnif condition satisfied then make a new column in dataframe in pyhonpandas add new column based on another dataframepython dataframe new column based on other columns with different rowpandas combine two dataframes based on columnadding a new column to a dataframe using if conditioncreate new column in dataframe using other columnspandas if column tru then show anotehr column valuespd conditional two columnspandas add column based on condition of other columnspandas create a new column using conditions of other columnspython new column if two columns are truecreate a df from 5b 28a 2c b 29 5dpandas series from dataframe with conditionadd new column that categorizes based on another column pandapandas add 2 columns based on other columnspandas create columns from other columnspandas apply condition of two columns pandas conditional create column based on another columnpandas dataframe create new column based on other columnspython create new column based on conditionpandas select columns by conditioncreating column conditional on oother column pandasdataframe compare two columns to add a new columnpython dataframe add column based on other columnspandas dataframe compare two columns and insert value to new columncreate columns with conditions in koalas dataframepandas add new column based on conditionconditional off of column in dfpandas create new column conditional on other columncreate dataframe with 2 columns pythoncreate dataframe with single columnpandas create column based on conditions of other columnsadd column to dataframe pandas conditionalpandas create a column based on another column multiple conditionscreate a new column in pandas based on multiple condition of another columndataframe create column based on conditionpandas dataframe add column condition from other dataframepandas combine 2 column values as new columselect data based on condition in two columns pandashow to add a conditional column pandashow to create new column in pandas dataframe based on conditionpandas conditionally create columncreate a column in pandas based on conditionpandas new column based on another columnif condition for column creation in pandashow to condition 2 columns in pandasapply if pandas two columnscreate a column based on condition pandasappend columns dataframe based on conditionpython how to conditionally create new column in pandas dataframeif in pandas in one linepython pandas add column with value from another columnpandas create a new column based on conditioncreate a new column based on other columns pandaspandas conditionally create new columncreate new column in pandas dataframe based on conditioncreate columns if statement pythondataframe create column from other columnsselect dataframe rows based on multiple conditions on columns pythoncreating column based on other columns pandascreate new column in data frame based on some conditionpandas new column conditional on other columnscreate new column with np wherepandas add column with conditionhow to create a new column based on other columns in pandashow to create a new column of number in pandas based on another columnadd new column in dataframe based on condition from anotherconditional create column pandaradd new column to dataframe without conditionadd column to dataframe from another dataframe based on conditionhow to make condition in new column df in pandasif column have value add value in other columndataframe add column conditional valuescreate a new column in pandas based on other columns condetionpandas create column 1 if else 0column value np where pandasapply if statement in a column of datasetadd specific columns if another column value meetes criteria pandaspandas create new column with if conditioncreate multiple columns in pandas based on conditionsmake new column based on other oclumns pandaspandas create a column based on another conditionhow to make a new column based on the values from other columns pandas else ifpandas if pandas new column from two columns when at least one of them has valueadding the value of one column to another if met conditions pandasdataafram colum and or conditionalpandas create a new column by adding 2 other columnshow to merge two dataframes in pandas based on one column valuepandas create column based on conditioncreate several columns based on condition pandasnp select with multiple conditionscreate new column based on another pandasadd column to dataframe based on conditionif else between two pandas dataframe add thord columncreate dataframe with existing columns of two dataframes inecreate new column pandas conditionalcreate a new column in pandas based on conditiondataframe create new columns based on two columnscreate new column in dataframe using calculations on other columnspandas new column conditionalcreate column given condition pandascreate new column pandas based on conditioncompare 2 dataframe and add new column pandascreate a new column in pandas based on other columns distributionadd conditional column pythonpython pandas create series based on conditionhow do i add a new column in dataframe based on the condition of some other colnumcreate two new columns in pandas dataframepandas create new column conditional on other columnshow to create a new column based on multiple condition pandaspandas create new column based on conditionhow to flag 0 or 1 if some condition is met in python dataframepandas calculate new column based on old column if elsepandas create column on conditionhow to make add a column to a dataframe in python conditionaldf set column conditional fastest way lambdapandas conditional columnpandas create column based on condition from another columnadd column pandas based on conditionhow to create new column in dataframe based on other cohow to create new column in dataframe based on other column valuescreate a column based on a conditional in pandascreate a new column that takes a specific value from another column if a condition is met 22python 22add new column with condition pandas dataframepython pandas create series with a conditionget a column from another conditional column pandasselect condition 2 column dataframeconditional statement for next column pandaspandas create series from dataframe with conditioncreate a column with condition pandascreate a new column based on condition in pandashow to add a new column with conditionals in pandasnumpy if else dataframepandas np where ifnew column based on condition pandascreate new column based on condition in pythondplyr add column based on another dataframeadd to column conditional pandascolumn value endsiwth conditional pandaspandas add new column to dataframe with conditionhow to make a new dataframe with same columnsdataframe column new value with conditionpandas apply two columnsnew column based on another column pandasconditionals for the same data column pandasi want to create a new column in the dataframe of true 2ffalse when another column is compared to a conditionpython pandas create series with conditionpandas add column based on another columnpandas assign value to multiple column based on conditioncreate new column based on another column value pandascreate a column with true if condition pandaspandas add column from one dataframe to another based on conditionpandas new column if conditionpandas if else columncreate new column in pandas based on two columnspandas add column based on other columnspython set conditional column valueadd new column based on condition pandasmultiple column condition pandasnew conditional column pandasadd new column in dataframe based on condition check two column condition dynamically pandasdataframe create data and column from other columnsadd column based on another column pandasnp where with multiple conditionscreate column with condition pandaspandas new column based on other columntcreate conditional column pandasadd column in pandas one value at timeadd a column of ones to dataframe pandasdisplay conditional column pandascreate column from np whereadd column based on condition pandaspandas combining two column with if statementspandas create new column with values based on another columnpandas create new column based on another columnpandas create a conditional columncreate pandas field into a if conditionpandas dataframe add new column based on other columnsmerge two columns in pandas dataframe based on conditionpandas fill column conditionallypandas add conditional column based on two other columnscreate new column based on other columns pandascreate a dataframe with two columns valueshow to create a column of a dataframe based on a conditionhow to add columns based on another column pandaspandas conditional functioncondition where 2 columns pandasadd a new column to a dataframe based on other columnshow do i add a new colnum in dataframe based on the condition of some other colnumpandas create new column based on other columnscreate a new column in pandas dataframe using if conditioncreate new column in pandas based on other columnspandas create new column based on multiple conditionnp where if else pandasadd new column based on condition on some other column in pandas how to make a column if there isn 27t one and add value if there is one in dataframe pandasnp select multiple conditionsadd a new column based on condition pandashow to create a new dataframe with if statementpandas add new column from another dataframe based on conditioncreate a new column in pandas based on other columnscombine two dataframes pandas based on columncreate new column in python based on conditioncreate pandas dataframe column with iffcreate new column based on another column pandaspandas create a new column based on another columnpandas create a new column based on comparison of two other columnscareate a new dataframe colum with conditions from other columsadd new column to dataframe pandas based on other columnspython add new column condition on 2 columns rowscan i make new column on base of condition in pandaspandas add column based on two other columnscreate two new columns panda dataframeadd conditional name columns pandascreate new column based on other columns condition pandascreate new column in pandas using where conditioncreate new column in dataframe python based on other columnsadd new columns in dataframe based on values of other columncreate column based on condition on two other columns pandaspandas create a new column by combining two columnscreate a column in python based on condition on aother columnpandas create new column based on other columns if elsepython add column with conditionpandas create a column value based on other columnspandas column value if elsecreate a new column based on condition in two dataframepandas add column to dataframe based on conditionadd column in pandas with if condiif statement for new column pandasconcatinate 2 column to one pandas with conditionpandas new column based on another column conditioncombine 1 column dataframe with another 1 cooumn dataframe pandaspandas new column from two columnspandas conditional column fillcreate pandas column with new values based on values in other columnspython pandas new column based on two condition columns conditioncreate a column in python based on condition on other columncreate a new dataframe with 2 columnsmultiple condition in np selectcreate a column based on condition and another dataframe pandashow to make conditions with 2 columns in pandasdataframe new column based on other columnscreate 2 column dataframe pandasgenerate a dataframe inline with ifpandas new column function of other columnspandas create dataframe from two columns if they are differentpython dataframe new column based on other columnscreate dataframe with columns for other dataframecreate new columns based on condition pandasdataframe add column based on other columnspandas create new column if conditionget conditional column pandaspandas create column conditionpandas dataframe create a new column from calculation of two other columns for each rowcreate new column from two columns pandaspython create new column based on other columnscreate new column based on two columns pandascreate new column ased in two other columns pandaspandas df add column from de combination of other columnspandas create series from dataframe column with conditionpandas set value of column either of two columns based on conditionhow to add a new column in pandas dataframe by applying some condition on different columncreate new column in pandas dataframe based on multiple conditionhow to apply conditional formatting to entire column based on another columnadd a column in pandas based on conditionpandas create new column based on other columns row beforepandas add column based on calculation from two other columnsin df if three columns data apply my condition create new columnadd new column pandas based on other columnspandas appply column dtype to another columncreate a new column based on another columns in pandascreate new column pandas based on condition and values from another columnpandas how to make a series with a conditionalhow to combine pandas columns for conditionpandas create new column based on condition of other columnspandas new column from other columnspandas dataframe add column with conditiondataframe create new column based on other columnspandas replace column with anotherpandas add new column based on other columnshow to add a data to a dataset in python in if elsepandas create two new columns from applypandas create new column based on other columns in listhow to add a conditional column in pandasif condition pandas new columnpython conditionally create new columnpandas merge two columns on conditionpython pandas create new column based on other columnspandas new column based on two columnsadd new column to dataframe based on conditionset value of column dataframe based on two other columnsadd a new column with values based on condition pandasnp where multiple conditions multiple resultspandas create many new columns based on conditionpandas create new column based on other dataframepandas combine two columns if not nullhow to create a column based on other columns in pandaspandas assign one column or the other depending on conditioncreate new column based on condition pandas fastest wayhow to create a new column based on condition in pandaslist all values in dataframe using if elsecolumn pandasadd a columns in pandas with if else condition from different columnsnumpy select multiple conditionsadd a column to a pandas dataframe based on an if else conditionpandas new column result of conditionalcreate dataframe with existing columns of two dataframeshow to create single columns as a datframe in pythonpandas create 3 new column based other row conditionadd new column to dataframe with conditioncreate new column in pandas dataframe based on other columnsdf where examples 2c setiting cnditionalpandas add new column with value from another dataframedf create new column based on other columnspandas add column with value from another columnpandas new column conditional valuedisplay only column with where clause from another column pandaspandas new column based on multiple conditionsadd new column in dataframe based on condition from another dataframenp select multiple conditions pandascreate a new integer column based on multiple condition pandascreate column with condition pandas if loopcreate multiple column based on condition of another column pandashow to create a conditional df colum from existing df columnpandas column to series with conditionpandas create new column based on two other columnsdf set column conditionalpandas add multiple columns based on conditionadd value new column to dataframe based on conditionconditional create column pandaspython pandas new column based on other columnspandas create new column with multiple if statementpandas make new column based on another columnif old column is between interval then create new column in pythoncreate pandas column based on other columnsif two columns are equal then add two other columns pandasdataframe create new column based on condition rpanda add new column based on other column valuescreate column based on condition pandasapply condition on two columns pandaspandas create column based on multiple conditionscreate new column based on condition pandas in other oclumncreate a conditional column pandasif column contains two values then create new column with value python dfpandas add value to column based on condition in rows new old datacreate new column based on contains condition pandascreate dataframe with 2 columnspandas create a column based on other columnsapplying two conditions pandas dataframepython conditionally create new column in pandas dataframepandas new column value based on another columncreate new column pandas based on column condition dataframe create new column based on condition for some rowscreate new column based on condition pandasand two columns pandasif one col 3d columns pandascreate a new column based on another column pandaspandas create dataframe with one column and two rowsmake new column based on condition pandaspandas merge rows based on conditionnew column pandas conditionaldataframe add column based on other columnpandas create a column from 2 different columns based on conditionadd column to dataframe based on mathematical conditions pandasadding a new column in pandas based on conditions of multiple columnsadd new column based on condition on some other column in pandas