python - 如何通过从所有其他列中减去第一列来创建新的数据框?

标签 python pandas dataframe calculated-columns difference

import pandas as pd
df = {'a': [1,1,1], 'b': [3,3,3,], 'c': [5,5,5,], 'd': [7,7,7], 'e': [9,9,9]}
df1 = pd.DataFrame(df, columns = ['a', 'b', 'c', 'd', 'e'])
dg = {'b': [2,2,2], 'c': [4,4,4], 'd': [6,6,6], 'e': [8,8,8]}
df2 = pd.DataFrame(dg, columns = ['b', 'c', 'd', 'e'])
df1
    a   b   c   d   e
0   1   3   5   7   9
1   1   3   5   7   9
2   1   3   5   7   9

df1 是我的原始数据框。我想通过从所有其他列中减去 a 列(取 a 列与所有其他列之间的差值)来创建另一个数据框。

df2
    b   c   d   e
0   2   4   6   8
1   2   4   6   8
2   2   4   6   8

df2 是结果。

非常感谢您的帮助。

最佳答案

df1.iloc[:,1:].sub(df1.a,axis=0)

    b   c   d   e
0   2   4   6   8
1   2   4   6   8
2   2   4   6   8

关于python - 如何通过从所有其他列中减去第一列来创建新的数据框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61969568/

相关文章:

python - 当catplot图是子图的一部分时,如何修改它的 "ylabel"属性?

python - 使用 Dask 导入大型 CSV 文件

python - 在虚拟环境中使用 sudo 执行脚本 - 找不到包

python - 波浪号登录 Pandas 数据框

python - 列出数据框列中存在的所有数据类型

python - pandas:使用 (row, col) 索引设置值

python - Pandas 值(value)错误: Cannot set a frame with no defined index and a value that cannot be converted to a Series

python - 将一个 pandas DataFrame 的副本合并到另一个 DataFrame 的每一行中?

python - 使用 pandas 识别最近的物体

python - __repr__ 与代表