简体   繁体   English

根据条件将列值除以列中的值

[英]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.因此,如果我在 A 列中有值 2015,那么 B 列中的所有值都应除以 A 中具有 2015 的行中的值。

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.它完全按照您的需要工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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