I have a dataset with 20 columns where I want to divide the first 10 by the last 10.
Here are some columns and rows of my dataset:
dict = {'X_1': {'A': 5, 'B': 27, 'C': 26, 'D': 71},
'X_2': {'A': 6, 'B': 51, 'C': 173, 'D': 183},
'Y_1': {'A': 479, 'B': 298, 'C': 598, 'D': 693},
'Y_2': {'A': 964, 'B': 474, 'C': 1854, 'D': 1855}
}
df = pd.DataFrame(dict)
Output:
X_1 X_2 Y_1 Y_2
A 5 6 479 964
B 27 51 298 474
C 26 173 598 1854
D 71 183 693 1855
I could just do it manually,
df["Z_1"] = df["X_1"] / df["Y_1"]
...
df["Z_n"] = df["X_n"] / df["Y_n"]
but I suspect there's a better solution to this.
Any suggestions?
You can do the division in one run using numpy:
df[df.filter(like='X').columns.str.replace('X', 'Z')] = df.filter(like='X').to_numpy() / df.filter(like='Y').to_numpy()
Result:
X_1 X_2 Y_1 Y_2 Z_1 Z_2
A 5 6 479 964 0.010438 0.006224
B 27 51 298 474 0.090604 0.107595
C 26 173 598 1854 0.043478 0.093312
D 71 183 693 1855 0.102453 0.098652
Use .iloc[]
indexing to slice your columns by number...
for i in range(10):
df.iloc[:,i] / df.iloc[:,10+i]
... then you can form the entire resulting dataframe with a generator expression:
pd.concat((df.iloc[:,i] / df.iloc[:,10+i] for i in range(10)), axis=1)
(There might be a shorter pandas way but I'm not aware of it).
Convert columns to MultiIndex and divide:
temp = df.copy()
temp.columns = temp.columns.str.split("_", expand=True)
temp
X Y
1 2 1 2
A 5 6 479 964
B 27 51 298 474
C 26 173 598 1854
D 71 183 693 1855
Divide X by Y:
temp = temp.X.div(temp.Y).add_prefix("Z_")
temp
Z_1 Z_2
A 0.010438 0.006224
B 0.090604 0.107595
C 0.043478 0.093312
D 0.102453 0.098652
Assign new values to original dataframe:
df.assign(**temp)
X_1 X_2 Y_1 Y_2 Z_1 Z_2
A 5 6 479 964 0.010438 0.006224
B 27 51 298 474 0.090604 0.107595
C 26 173 598 1854 0.043478 0.093312
D 71 183 693 1855 0.102453 0.098652
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.