I have a value in a field t1_months that is set to calculate the number of months that spending is occurring. I'm confident in that value and would like to use that as a limit for the number of allotments to be populated.
With this code I am getting the error ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
So that leads me to think that I need to retool this line as this is the line that the code stopped on.
if count <= counter:
How can i go about creating a conditional like this but still be able to use the np.where clause?
for month_num in range(1, 13):
count = 0
# counter = df['t1_months']
if count <= df['t1_months']:
if count <= counter:
df['t1_' + str(month_num) + '/1/2016'] = np.where(
(df["StartMonthYear"] <= pd.to_datetime(str(month_num) + '/1/2016'))
& (df["EndMonthYear"] >= pd.to_datetime(str(month_num) + '/1/2016')), df["t1_allotment"], '0.0')
count += 1
So if I have the dataframe of df = pd.DataFrame( {'t1_months' : [12,10,6,7]})
how can I use 12 as the check in the first row to only populate 12 allocations, 10 in the second row, 6 in the third and 7 in the last?
Expected output would be something like this:
t1_months 1/1/2016 ... 6/1/2016 7/1/2016 8/1/2016 9/1/2016 10/1/2016 11/1/2016 12/1/2016
12 500 ... 500 500 500 500 500 500 500
10 500 ... 500 500 500 500 500 500
500 500 0 0
6 500 ... 500 500 500 500 0 0 0 0 0 0
7 500 ... 500 500 500 500 500 0 0 0 0 0
df['t1_months']is a whole column in Pandas so how is the comparison supposed to be done if that column contains multiple different values?if count <= df['t1_months']count is an int, df['t1_months] is a series. These cannot be comparedif 0 is less than or equal to [1, 2, 12, 0, 12, 1, 2, 3, 4, 12, 0]how can you possibly answer that question? This is the problem with your code. What you may want to do is work out how to iterate over the series. But that is not what the question is about. If you want a more specific answer then you should post data and expected output. Keep trying :-)