简体   繁体   English

在熊猫队中用NBA统计数据创建一个三双栏目。

[英]Create a triple-double column in Pandas with NBA stats

I'm running into an issue with creating a very specific kind of boolean column in pandas. 我在熊猫中创建一种非常特定的布尔列时遇到问题。 I'm working with NBA data and I want to create a column for when a player gets a triple double. 我正在处理NBA数据,我想为一名球员获得三重双打创建一列。 This code works: 此代码有效:

james_harden['trip_dub'] = (james_harden['points'] >= 10) & (james_harden['rebounds' >= 10) & (james_harden['assists'] >= 10)

james_harden['trip_dub'] = james_harden['trip_dub'].map(lambda x: 1 if x == True else 0)

However a triple double can be achieved with points, rebounds, and assists...points, rebounds, and blocks...rebounds, blocks, and assists...etc 但是三分可以通过得分,篮板和助攻来实现……点,篮板和盖帽...篮板,盖帽和助攻...等等

I'm wondering if it's possible to write code to check if any combination of three columns out of a total of five are greater than or equal to 10... 我想知道是否有可能编写代码来检查总共五列中三列的任何组合是否大于或等于10 ...

So if i have columns a, b, c, d, e how do I check if (a >= 10 and b>= 10 and c>=10) OR (a >= 10 and d>=10 and e >= 10) ? 因此,如果我有a,b,c,d,e列,如何检查(a >= 10 and b>= 10 and c>=10) OR (a >= 10 and d>=10 and e >= 10)

Assuming you have 5 statistics, and a triple-double is defined as at least 10 points in at least 3 statistics, you can use pd.DataFrame.sum along axis=1 : 假设您有5个统计信息,并且在至少3个统计信息中至少有10个点被定义为三重对偶,则可以沿axis=1使用pd.DataFrame.sum

stats = ['points', 'rebounds', 'assists', 'blocks', 'steals']
james_harden['trip_dub'] = (james_harden[stats] >= 10).sum(1) >= 3

The idea is james_harden[stats] >= 10 represents a Boolean dataframe and calling sum on it along columns sums the number of True values in each row . 这个想法是james_harden[stats] >= 10代表一个布尔数据james_harden[stats] >= 10 ,并在其上沿列调用sum 每行True值的数量。 We then need only check the numbers in each row are greater than or equal to 3 . 然后,我们只需要检查每行中的数字是否大于或等于3

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

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