简体   繁体   English

pandas 为 dataframe 的每一行计算新值的方法是什么?

[英]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.

相关问题 有没有办法向pandas数据框添加新列,将新列的每个唯一值附加到数据帧的每个现有行? - Is there a way to add a new column to a pandas dataframe, appending each unique value of the new column to every existing row of the dataframe? 在 Pandas dataframe 中,如何根据每一行的值在 append 中创建一个新的 True / False 列? - In Pandas dataframe, how to append a new column of True / False based on each row's value? 循环通过 Pandas dataframe 在条件为真的每一行中使用顺序计数值的最佳方法是什么? - What is best way to loop through Pandas dataframe employing a sequentially counted value in each row where condition is true? 将熊猫数据框的每一行写入一个新的文本文件 - pythonic方式 - Write each row of pandas dataframe into a new text file - pythonic way 通过对每一行进行操作,在数据框中创建列的“pandas”方法是什么? - What is the `pandas` way to create a column in a dataframe by operating on each row? 如何在数据框中拆分一列并将每个值存储为新行(以熊猫为单位)? - How to split a column in a dataframe and store each value as a new row (in pandas)? 是否有 pandas 方法通过特定列值为每一行添加 dataframe - Is there a pandas way to add a dataframe for each row by a specific column value 告诉熊猫DataFrame中丢失的行的最佳方法是什么? - What's the best way to tell the missing row in pandas DataFrame? 计算 dataframe 中每一行的概率 - Computing probability for each row in a dataframe 对熊猫数据框中每一行进行排序的最快方法 - Fastest way to sort each row in a pandas dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM