Differencing all other(not selected) columns in a dataframe by another column
Question:
I have a data frame with the following structure.
Index1 Index2 -2 -1 0 1 2
0 A W 20 25 28 29 18
1 B X 21 20 26 21 27
2 C Y 19 19 20 17 17
3 D Z 18 17 15 18 18
I want to subtract the ‘0’ column from all columns except for the index columns and create new columns with the result, titled using the column name and a suffix. I successfully managed to do this:
Index1 Index2 -2 -1 0 1 2 -1 ph -2 ph 1 ph 2 ph
0 A W 20 25 28 29 18 -3 -8 1 -10
1 B X 21 20 26 21 27 -6 -5 -5 1
2 C Y 19 19 20 17 17 -1 -1 -3 -3
3 D Z 18 17 15 18 18 2 3 3 3
The code to recreate the example:
import pandas as pd
# Create DataFrame
data = {'Index1': ['A', 'B', 'C', 'D'],
'Index2': ['W', 'X', 'Y', 'Z'],
'-2': [20, 21, 19, 18],
'-1': [25, 20, 19, 17],
'0': [28, 26, 20, 15],
'1': [29, 21, 17, 18],
'2': [18, 27, 17, 18]
}
df = pd.DataFrame(data)
df = df.assign(**df[['-1','-2', ,'1', '2']].sub(df['0'], axis=0).add_suffix(' ph'))
print(df)
However, the columns (except for the indexes) can vary (so could be -3,-2,1,0,1 etc.) So I want to update my code to say: subtract column ‘0’ from all columns except for the indexes (Index1 and Index2).
I tried this (and a few other alternatives):
df = df.assign(**df[~['Index1','Index2']].sub(df['0'], axis=0).add_suffix(' ph'))
But didn’t seem to figure it out
Answers:
Drop the required columns then subtract and join the result back
c = ['Index1', 'Index2', '0']
df.join(df.drop(columns=c).sub(df['0'], axis=0).add_suffix(' ph'))
Result
Index1 Index2 -2 -1 0 1 2 -2 ph -1 ph 1 ph 2 ph
0 A W 20 25 28 29 18 -8 -3 1 -10
1 B X 21 20 26 21 27 -5 -6 -5 1
2 C Y 19 19 20 17 17 -1 -1 -3 -3
3 D Z 18 17 15 18 18 3 2 3 3
I have a data frame with the following structure.
Index1 Index2 -2 -1 0 1 2
0 A W 20 25 28 29 18
1 B X 21 20 26 21 27
2 C Y 19 19 20 17 17
3 D Z 18 17 15 18 18
I want to subtract the ‘0’ column from all columns except for the index columns and create new columns with the result, titled using the column name and a suffix. I successfully managed to do this:
Index1 Index2 -2 -1 0 1 2 -1 ph -2 ph 1 ph 2 ph
0 A W 20 25 28 29 18 -3 -8 1 -10
1 B X 21 20 26 21 27 -6 -5 -5 1
2 C Y 19 19 20 17 17 -1 -1 -3 -3
3 D Z 18 17 15 18 18 2 3 3 3
The code to recreate the example:
import pandas as pd
# Create DataFrame
data = {'Index1': ['A', 'B', 'C', 'D'],
'Index2': ['W', 'X', 'Y', 'Z'],
'-2': [20, 21, 19, 18],
'-1': [25, 20, 19, 17],
'0': [28, 26, 20, 15],
'1': [29, 21, 17, 18],
'2': [18, 27, 17, 18]
}
df = pd.DataFrame(data)
df = df.assign(**df[['-1','-2', ,'1', '2']].sub(df['0'], axis=0).add_suffix(' ph'))
print(df)
However, the columns (except for the indexes) can vary (so could be -3,-2,1,0,1 etc.) So I want to update my code to say: subtract column ‘0’ from all columns except for the indexes (Index1 and Index2).
I tried this (and a few other alternatives):
df = df.assign(**df[~['Index1','Index2']].sub(df['0'], axis=0).add_suffix(' ph'))
But didn’t seem to figure it out
Drop the required columns then subtract and join the result back
c = ['Index1', 'Index2', '0']
df.join(df.drop(columns=c).sub(df['0'], axis=0).add_suffix(' ph'))
Result
Index1 Index2 -2 -1 0 1 2 -2 ph -1 ph 1 ph 2 ph
0 A W 20 25 28 29 18 -8 -3 1 -10
1 B X 21 20 26 21 27 -5 -6 -5 1
2 C Y 19 19 20 17 17 -1 -1 -3 -3
3 D Z 18 17 15 18 18 3 2 3 3