I have a pandas dataframe like:
color cost temp
0 blue 12.0 80.4
1 red 8.1 81.2
2 pink 24.5 83.5
and I want to create a "ladder" or a "range" of costs for every row at 50 cent increments, from $0.50 below the current cost to $0.50 above the current cost. My current code is similar to the follow:
incremented_prices = []
df['original_idx'] = df.index # To know it's original label
for row in df.iterrows():
current_price = row['cost']
more_costs = numpy.arange(current_price-1, current_price+1, step=0.5)
for cost in more_costs:
row_c = row.copy()
row_c['cost'] = cost
incremented_prices.append(row_c)
df_incremented = pandas.concat(incremented_prices)
And this code will produce a DataFrame like:
color cost temp original_idx
0 blue 11.5 80.4 0
1 blue 12.0 80.4 0
2 blue 12.5 80.4 0
3 red 7.6 81.2 1
4 red 8.1 81.2 1
5 red 8.6 81.2 1
6 pink 24.0 83.5 2
7 pink 24.5 83.5 2
8 pink 25.0 83.5 2
In the real problem, I will make ranges from -$50.00 to $50.00 and I find this really slow, is there some faster vectorized way?