Pandas: Divide Column Data By Number If Row Of Next Column Contains Certain Value
I have a dataframe that consists of three columns qty unit_of_measure qty_cal 3 nodes nan 4 nodes nan 5 nodes
Solution 1:
Use np.where
:
df['qty_cal'] = np.where(df['unit_of_measure'] == 'nodes', df['qty'], df['qty']/16)
Solution 2:
The statement ppn_df['unit_of_measure']
returns a series (a column) with all the values in it, not a single item. One way to do this is with an apply
or a map
Try this
ppn_df.qty_cal = ppn_df.apply(lambda x: x['qty'] if x['unit_of_measure'] == 'nodes' else x['qty'] / 16, axis=1)
This function will execute the lambda
function for each row in the series
Post a Comment for "Pandas: Divide Column Data By Number If Row Of Next Column Contains Certain Value"