简体   繁体   English

如何在熊猫中将一列的值与另一列的值相除/相乘?

[英]How to divide/multiply value of a column with value of another column in pandas?

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? 然后将所有系列串联起来,这样做的pythonic方法是什么? How can I do this using Map/lambda operation? 如何使用Map / lambda操作做到这一点?

IIUC you can use: 您可以使用IIUC:

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. 我相信您随后将需要重新计算FG%和3P%列。 This will do the division on your original dataframe, leaving MP, FG% and 3P% untouched. 这将对原始数据帧进行分割,而MP,FG%和3P%保持不变。

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!!! ps勇士!!!

No, concatenating the new series is plenty idiomatic. 不,串联新系列是很惯用的。

You can also use df['newcol'] = ... to build up what you want. 您还可以使用df['newcol'] = ...来构建所需的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM