简体   繁体   中英

Dividing column values with value in the column based on a condition

How I divide column values with one of the values in the column based on a value in other column. So if I have value 2015 in the column A then all the values in column B should be divided with the value in the row with 2015 in A.

df = pd.DataFrame({"A" : (2014, 2015, 2016), "B" : (100, 200, 300)})
???
result = pd.DataFrame({"A" : (2014, 2015, 2016), "B" : (0.5, 1, 1.5)})
df['B'] =  df['B']/df['A']

It works exacly how you need I think.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM