Guest User

Untitled

a guest
Jan 22nd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. in[1]:
  2.  
  3. import pandas as pd
  4.  
  5. def somefunc(input1, input2):
  6. output1 = 1
  7. output2 = 2
  8.  
  9. return [output1, output2]
  10.  
  11.  
  12. d = {'col1': ['A1', 'B1'], 'col2': ['A2', 'B2']}
  13. df = pd.DataFrame(data=d)
  14.  
  15. df[['col3', 'col4']] = df.apply(lambda x: somefunc(x['col1'], x['col2']),
  16. axis=1)
  17.  
  18. print df
  19.  
  20. out[1]:
  21.  
  22. col1 col2 col3 col4
  23. 0 A1 A2 1 2
  24. 1 B1 B2 1 2
  25.  
  26. in[2]:
  27.  
  28. import pandas as pd
  29.  
  30. def somefunc(input1, input2):
  31. output1 = 1
  32. output2 = 2
  33. output3 = 3
  34.  
  35.  
  36. return [output1, output2, output3]
  37.  
  38.  
  39. d = {'col1': ['A1', 'B1'], 'col2': ['A2', 'B2']}
  40. df = pd.DataFrame(data=d)
  41.  
  42. df[['col3', 'col4', 'col5']] = df.apply(lambda x: somefunc(x['col1'], x['col2']),
  43. axis=1)
  44.  
  45. print df
  46.  
  47. out[2]:
  48. "ValueError: Columns must be same length as key"
  49.  
  50. KeyError: "['col3' 'col4' 'col5'] not in index"
Add Comment
Please, Sign In to add comment