[英]What's the pandas way of computing a new value for each row of a dataframe?
I have a dataframe like this:我有一个这样的 dataframe:
name upvotes posts
0 Britt 4 232
1 Henry 1 152
...
9 Kevin 1 48
I want to create a new column, let's call it clout
, that is a function of a user's score and posts.我想创建一个新列,我们称之为clout
,即用户分数和帖子的 function。
In standard fare Python, if this was a list of dictionaries, I would approach the problem iteratively as follows:在标准票价 Python 中,如果这是一个字典列表,我将按如下方式迭代处理该问题:
for row in myListOfDicts:
row['clout'] = computeClout(row['upvotes'],row['posts'])
But this approach seems wrong in Pandas based off of this answer: https://stackoverflow.com/a/55557758/4382391但是根据这个答案,这种方法在 Pandas 中似乎是错误的: https://stackoverflow.com/a/55557758/4382391
So what should I be doing in this case?那么在这种情况下我应该怎么做呢?
You can try你可以试试
df['clout' ] = df[['upvotes', 'Posts' ]].apply(computeClout, axis=1)
You can use apply as following您可以按以下方式使用申请
df['clout'] = df.apply(lambda row: computeClout(row['upvotes'],row['posts']), axis=1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.