0

I have a dataframe like this:

     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.

In standard fare Python, if this was a list of dictionaries, I would approach the problem iteratively as follows:

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

So what should I be doing in this case?

Null Salad
  • 765
  • 2
  • 16
  • 31
  • 1
    For a general `computeClout` function, that's the only way. You can try to recode your function so as it can take series as input and then you can do `df['clout'] = computeClout(df['upvotes'], df['posts'])`. – Quang Hoang Nov 13 '20 at 20:47

2 Answers2

2

You can try

df['clout' ] = df[['upvotes', 'Posts' ]].apply(computeClout, axis=1) 
Renaud
  • 2,709
  • 2
  • 9
  • 24
1

You can use apply as following

df['clout'] = df.apply(lambda row: computeClout(row['upvotes'],row['posts']), axis=1)
rpanai
  • 12,515
  • 2
  • 42
  • 64