我有一个如下的数据 DataFrame:
import pandas as pd
data = {
"A": [42, 38, 39, 23],
"B": [45, 30, 15, 65],
"C": [60, 50, 25, 43],
"D": [12, 70, 35, 76],
"E": [87, 90, 45, 43],
"F": [40, 48, 55, 76],
"G": [58, 42, 85, 10],
}
df = pd.DataFrame(data)
print(df)
输出:
A B C D E F G
0 42 45 60 12 87 40 58
1 38 30 50 70 90 48 42
2 39 15 25 35 45 55 85
3 23 65 43 76 43 76 10
在此处,我需要从列 B 中减去列 C 到列 G 的所有值,然后加上列 A 的值。
例如:
df['C'] = df['C'] - df['B'] + df['A']
df['D'] = df['D'] - df['B'] + df['A']
df['E'] = df['E'] - df['B'] + df['A']
# ... (以及对其他列进行类似操作)
如何用简单或单行命令实现这个操作?