in[1]:
import pandas as pd
def somefunc(input1, input2):
output1 = 1
output2 = 2
return [output1, output2]
d = {'col1': ['A1', 'B1'], 'col2': ['A2', 'B2']}
df = pd.DataFrame(data=d)
df[['col3', 'col4']] = df.apply(lambda x: somefunc(x['col1'], x['col2']),
axis=1)
print df
out[1]:
col1 col2 col3 col4
0 A1 A2 1 2
1 B1 B2 1 2
somefunc:
- Gets data from json in url with arguments input1 and input2 and puts it into a
new dataframe df2
- Creates output1, output2 with values from df2
- Deletes dataframe when output vaiables is calulated to save RAM
This executes perfectly, but I want to return 3 values which return an error
in[2]:
import pandas as pd
def somefunc(input1, input2):
output1 = 1
output2 = 2
output3 = 3
return [output1, output2, output3]
d = {'col1': ['A1', 'B1'], 'col2': ['A2', 'B2']}
df = pd.DataFrame(data=d)
df[['col3', 'col4', 'col5']] = df.apply(lambda x: somefunc(x['col1'], x['col2']),
axis=1)
print df
out[2]:
"ValueError: Columns must be same length as key"
The error is different from my actual program, but I guess it correlates;
KeyError: "['col3' 'col4' 'col5'] not in index"
Why does this work with one and two outputs but not three?
Python 2.7.14