My data frame looks liks this
Minutes Played, Points, Assists
MP PTS TRB AST FG% BLK 3P%
0 2810 793 678 117 0.485 74 0.315
1 263 101 30 19 0.402 7 0.385
2 4241 1170 1178 144 0.548 201 0.000
I want to convert that data frame to data-frame with these columns
Points/Minutes, Assists/Minutes
Basically first column is total-minutes played, I want to covert all of the remaining stats to per minute basis.
Right I am doing
input_data['PTS']/input_data['MP']
and then I am concatenating all of the series, what is the pythonic way of doing this? How can I do this using Map/lambda operation?
IIUC you can use:
print input_data
MP PTS TRB AST FG% BLK 3P%
0 2810 793 678 117 0.485 74 0.315
1 263 101 30 19 0.402 7 0.385
2 4241 1170 1178 144 0.548 201 0.000
input_data['A'] = input_data['PTS']/input_data['MP']
input_data['B'] = input_data['AST']/input_data['MP']
print input_data
MP PTS TRB AST FG% BLK 3P% A B
0 2810 793 678 117 0.485 74 0.315 0.282206 0.041637
1 263 101 30 19 0.402 7 0.385 0.384030 0.072243
2 4241 1170 1178 144 0.548 201 0.000 0.275878 0.033954
print pd.DataFrame({'A': input_data['A'],'B': input_data['B']}, index=input_data.index)
A B
0 0.282206 0.041637
1 0.384030 0.072243
2 0.275878 0.033954
Divide all columns in the dataframe except the first by the first column.
df.iloc[:, ].apply(lambda s: s / df.iloc[:, 0])
PTS TRB AST FG% BLK 3P%
0 0.282206 0.241281 0.041637 0.000173 0.026335 0.000112
1 0.384030 0.114068 0.072243 0.001529 0.026616 0.001464
2 0.275878 0.277765 0.033954 0.000129 0.047394 0.000000
This also works:
df.iloc[:, 1:].div(df.iloc[:, 0].values, axis=0)
I believe you will then need to recalculate your FG% and 3P% columns. This will do the division on your original dataframe, leaving MP, FG% and 3P% untouched.
df.iloc[:, [1, 2, 3, 5]] = df.iloc[:, [1, 2, 3, 5]].div(df.iloc[:, 0].values, axis=0)
>>> df
MP PTS TRB AST FG% BLK 3P%
0 2810 0.282206 0.241281 0.041637 0.485 0.026335 0.315
1 263 0.384030 0.114068 0.072243 0.402 0.026616 0.385
2 4241 0.275878 0.277765 0.033954 0.548 0.047394 0.000
ps Go Warriors!!!
No, concatenating the new series is plenty idiomatic.
You can also use df['newcol'] = ...
to build up what you want.
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.