I am trying to find a shortcut to assigning a pandas series from elements in a list.
My code:
import pandas as pd
df=pd.DataFrame([[1,2,2183],[1,4,2235],[2,3,6123],[3,4,4213]],columns=['month','staff','sales'])
goals=[1346,4456,4574]
df['goals']=goals[df['month']-1]
The last line throws an error: TypeError: list indices must be integers or slices, not Series
Desired output looks like this:
month staff sales goals
0 1 2 2183 1346
1 1 4 2235 1346
2 2 3 6123 4456
3 3 3 4213 4574
This is a general case, and in the future, the goals array might actually be multidimensional.
df['goals']=goals?