问题
I have a dataframe that consists of three columns
qty unit_of_measure qty_cal
3 nodes nan
4 nodes nan
5 nodes nan
6 cores nan
7 nodes nan
10 cores nan
3 nodes nan
I would like to add a condition to populate qty_cal.
The condition is if unit_of_measure is equal to "nodes" populate the row value of qty into qty_cal
If it's "cores" divide qty value by 16 and populate qty_cal
The code I have tried is,
if ppn_df['unit_of_measure'] == 'Nodes':
ppn_df['qty']
elif ppn_df['unit_of_measure'] =='Cores':
ppn_df['qty'] / 16
I'm getting an error of
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
I'm not sure why I'm getting this value error. I don't understand why the if statement is ambiguous.
Can anyone care to explain?
回答1:
Use np.where
:
df['qty_cal'] = np.where(df['unit_of_measure'] == 'nodes', df['qty'], df['qty']/16)
回答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
来源:https://stackoverflow.com/questions/57299343/pandas-divide-column-data-by-number-if-row-of-next-column-contains-certain-valu