Python DataFrame:根据最小和最大范围添加两列?
我有两个df,一个是Python DataFrame:根据最小和最大范围添加两列?,python,pandas,numpy,dataframe,Python,Pandas,Numpy,Dataframe,我有两个df,一个是价格,另一个是价格范围 price_df name price 0 anthony 5 1 paul 16 2 marcus 25 3 bruno 45 range_df add_amount min max 0 10
价格
,另一个是价格范围
price_df
name price
0 anthony 5
1 paul 16
2 marcus 25
3 bruno 45
range_df
add_amount min max
0 10 0 10
1 20 10 20
2 30 20 30
3 40 30 50
如果price
列属于该类别,我将尝试将金额添加到price中。
例如:
name
-marcus
的价格25
,介于20-30之间,因此在price
中添加30
Expected output
name price final_amount
0 anthony 5 15
1 paul 16 36
2 marcus 25 55
3 bruno 45 85
我想您可以使用pd.cut
:
price_bins = [range_df['min'][0]] + list(range_df['max'])
price_df['final_amount'] = price_df['price'] + pd.cut(price_df['price'],
bins= price_bins,
labels=range_df['add_amount']
).astype(float)
输出:
name price final_amount
0 anthony 5 15.0
1 paul 16 36.0
2 marcus 25 55.0
3 bruno 45 85.0
您可以使用merge\u asof
并设置前进方向:
s = (pd.merge_asof(price_df, range_df, left_on="price", right_on="max", direction="forward")
.drop(["min", "max"], axis=1))
print (s.assign(final_amount=s["price"]+s["add_amount"]))
name price add_amount final_amount
0 anthony 5 10 15
1 paul 16 20 36
2 marcus 25 30 55
3 bruno 45 40 85
解决问题的另一种方法:
l = [] # creat a list for to add final amount
for i in price_df['price']:
for j in range(len(range_df['min'])):
if i < range_df['max'][j] and i > range_df['min'][j]:
l.append(i + range_df['add_amount'][j]) # append the variable between min a max to l
else:
print('-->', range_df['add_amount'][j])
price_df['final_amount'] = l # make a coulmn named final_amount
l=[]#创建一个列表,用于添加最终金额
对于价格中的i_df[‘价格’]:
对于范围内的j(len(范围_df['min']):
如果irange_df['min'][j]:
l、 追加(i+range_df['add_amount'][j])#追加最小值a最大值到l之间的变量
其他:
打印('-->',范围[添加金额][j])
价格df['final_amount']=l#制作一个名为final_amount的表格