lets say I have a dataframe like below
+------+------+------+-------------+
| A | B | C | devisor_col |
+------+------+------+-------------+
| 2 | 4 | 10 | 2 |
| 3 | 3 | 9 | 3 |
| 10 | 25 | 40 | 10 |
+------+------+------+-------------+
what would be the best command to apply a formula using values from the devisor_col. Do note that I have thousand of column and rows.
the result should be like this:
+------+------+------+-------------+
| A | B | V | devisor_col |
+------+------+------+-------------+
| 1 | 2 | 5 | 2 |
| 1 | 1 | 3 | 3 |
| 1 | 1.5 | 4 | 10 |
+------+------+------+-------------+
I tried using apply map but I dont know why I cant apply it to all columns.
modResult = my_df.applymap(lambda x: x/x["devisor_col"]))
2条答案
按热度按时间juud5qan1#
IIUC,在
axis=0
上使用pandas.DataFrame.divide
:#输出:
如果您只需要除法的结果,请使用下列方式:
或者,如果要覆盖旧列,请使用
pandas.DataFrame.join
:可以将
my_df.filter(like="Col")
替换为my_df.loc[:, my_df.columns!="devisor_col"]
。vlju58qv2#
您可以尝试使用
.loc