简体   繁体   English

如何将我的 function 应用到 dataframe 的第一行?

[英]How to apply my function to the first row of a dataframe?

def calcScore(p):
   if p[0] > p[1]:
    x = 3
    y = 0
   elif p[0] == p[1]:
    x = 1
    y = 1
   else:
    x = 0
    y = 3

   return x,y

How would I apply this function to the first row of my dataframe?如何将此 function 应用于我的 dataframe 的第一行? I know how to apply it to the whole dataframe but can't seem to apply it to the first row only?我知道如何将其应用于整个 dataframe 但似乎不能仅将其应用于第一行? Below is what I did with the whole dataframe.下面是我对整个 dataframe 所做的。 I am new to python so please forgive silly or stupid mistakes.我是 python 的新手,所以请原谅愚蠢或愚蠢的错误。 Thank you.谢谢你。 :) :)

  result =(prem[['FTHG','FTAG']].apply(calcScore, axis = 1))
  print(result)

apply is for applying a function to all rows or columns. apply用于将 function 应用于所有行或列。 If you just want one you can just do:如果你只想要一个,你可以这样做:

result = calcScore(perm.iloc[0, ['FHG', 'FtAG']])

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

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