Pandas: Divide column data by number if row of next column contains certain value

雨燕双飞 提交于 2021-02-09 11:10:03

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!