I have the following code that.
import pandas as pd
def generate_list(row):
return [row['A']*2, row['B']*3]
def main():
data = {
'A':[0, 2, 3],
'B':[4, 15, 6]}
df = pd.DataFrame(data)
# applying function to each row in
df['C'], df['D'] = df.apply(lambda row: generate_list(row), axis=1)
if __name__ == '__main__':
main()
The code simply applies a function to each row of my df and the results returned as a list. I am trying to add each element of my returned list to two new columns. However, I get the following error:
df['C'], df['D'] = df.apply(lambda row: generate_list(row), axis=1)
ValueError: too many values to unpack (expected 2)
df[['C', 'D']] = df.apply(lambda row: generate_list(row), axis=1).tolist()