I have a df named df4,you can get it buy following code:
df4s = """
contract RB BeginDate ValIssueDate EndDate Valindex0 48 46 47 49 50
2 A00118 46 19850100 19880901 99999999 50 1 2 3 7 7
3 A00118 47 19000100 19880901 19831231 47 1 2 3 7 7
5 A00118 47 19850100 19880901 99999999 50 1 2 3 7 7
6 A00253 48 19000100 19820101 19811231 47 1 2 3 7 7
7 A00253 48 19820100 19820101 19841299 47 1 2 3 7 7
8 A00253 48 19850100 19820101 99999999 50 1 2 3 7 7
9 A00253 50 19000100 19820101 19781231 47 1 2 3 7 7
10 A00253 50 19790100 19820101 19841299 47 1 2 3 7 7
11 A00253 50 19850100 19820101 99999999 50 1 2 3 7 7
"""
df4 = pd.read_csv(StringIO(df4s.strip()), sep='\s+',
dtype={"RB": int, "BeginDate": int, "EndDate": int,'ValIssueDate':int,'Valindex0':int})
Out put would be:
contract RB BeginDate ValIssueDate EndDate Valindex0 48 46 47 49 50
2 A00118 46 19850100 19880901 99999999 50 1 2 3 7 7
3 A00118 47 19000100 19880901 19831231 47 1 2 3 7 7
5 A00118 47 19850100 19880901 99999999 50 1 2 3 7 7
6 A00253 48 19000100 19820101 19811231 47 1 2 3 7 7
7 A00253 48 19820100 19820101 19841299 47 1 2 3 7 7
8 A00253 48 19850100 19820101 99999999 50 1 2 3 7 7
9 A00253 50 19000100 19820101 19781231 47 1 2 3 7 7
10 A00253 50 19790100 19820101 19841299 47 1 2 3 7 7
11 A00253 50 19850100 19820101 99999999 50 1 2 3 7 7
I'm trying to build a new column by following logic,the value of new column will base on 2 existed columns' values :
def test(RB):
n=1
for i in np.arange(RB,50):
n = n * df4[str(i)].values
return n
vfunc=np.vectorize(test)
df4['n']=vfunc(df4['RB'].values)
And then received error:
res = array(outputs, copy=False, subok=True, dtype=otypes[0])
ValueError: setting an array element with a sequence.
df4[str(i)].valuesis an array so your return ofn(assuming RB is low enough that you do loop) is an array like:[6 6 6 6 6 6 6 6 6]vectorize is attempting to assign this back to a 1D array. Are you looking to create a 2d array here?vectorize, not the asisgnment to the dataframe column.