-1

So I have a pandas DataFrame that has several columns that contain values I'd like to use to create new columns using a function I've defined. I'd been planning on doing this using Python's List Comprehension as detailed in this answer. Here's what I'd been trying:

df['NewCol1'], df['NewCol2'] = [myFunction(x=row[0], y=row[1]) for row in zip(df['OldCol1'], df['OldCol2'])]

This runs correctly until it comes time to assign the values to the new columns, at which point it fails, I believe because it hasn't been iteratively assigning the values and instead tries to assign a constant value to each column. I feel like I'm close to doing this correctly, but I can't quite figure out the assignment.

EDIT:

The data are all strings, and the function performs a fetching of some different information from another source based on those strings like so:

def myFunction(x, y):
    # read file based on value of x
    # search file for values a and b based on value of y
    return(a, b)

I know this is a little vague, but the helper function is fairly complicated to explain.

The error received is:

ValueError: too many values to unpack (expected 4)
jjack
  • 99
  • 7

1 Answers1

1

You can use zip()

df['NewCol1'], df['NewCol2'] = zip(*[myFunction(x=row[0], y=row[1]) for row in zip(df['OldCol1'], df['OldCol2'])])
deadshot
  • 8,881
  • 4
  • 20
  • 39
  • This worked, thank you! Can I ask what the asterisk after the outer call to `zip()` is for? – jjack Aug 31 '20 at 19:51
  • 1
    this will help [What does the star operator mean, in a function call?](https://stackoverflow.com/questions/2921847/what-does-the-star-operator-mean-in-a-function-call) – deadshot Aug 31 '20 at 19:52