I want to do something like this, but it throws an error:
df.apply(lambda row: row / row[2] )
My input data is the following:
Index | Col1 | Col2 | Col3 |
---|---|---|---|
8206731245 | 1829 | 8440.19 | 140 |
6523960772 | 12452 | 523065.36 | 138 |
2546262822 | 14889 | 144067.84 | 138 |
Expected output:
Index | Col1 | Col2 | Col3 |
---|---|---|---|
8206731245 | 1829/140 | 8440.19/140 | 140/140 |
6523960772 | 12452/138 | 523065.36/138 | 138/138 |
2546262822 | 14889/138 | 144067.84/138 | 138/138 |
CodePudding user response:
Use something like:
pd.concat([df[["Index"]],df.drop(columns=["Index"]).apply(lambda row: row / row["Col3"] ,axis=1)], axis=1)
Output
Index Col1 Col2 Col3
8206731245 13.064286 60.287071 1.0
6523960772 90.231884 3790.328696 1.0
2546262822 107.891304 1043.969855 1.0
CodePudding user response:
df.apply()
is done over columns by default. Use axis=1
to apply over rows.
df.apply(lambda row: row / row[2], axis=1)
Col1 Col2 Col3
Index
8206731245 13.064286 60.287071 1.0
6523960772 90.231884 3790.328696 1.0
2546262822 107.891304 1043.969855 1.0
(I'm assuming your "Index" column represents the index.)
CodePudding user response:
Instead of apply
, you could use div
on axis=0
to leverage vectorized division:
out = df[['Index']].join(df.drop(columns='Index').div(df['Col3'], axis=0))
Or, if "Index" is the index, not a column (and it seems likely that's the case since you index row[2]
), then you can simply use:
out = df.div(df['Col3'], axis=0)
If you don't want to specify the column name, you could also use iloc
to use the location:
out = df.div(df.iloc[:, 2], axis=0)
Output:
Index Col1 Col2 Col3
0 8206731245 13.064286 60.287071 1.0
1 6523960772 90.231884 3790.328696 1.0
2 2546262822 107.891304 1043.969855 1.0