我试图在给定另一个pandas数据帧中的参数列表的情况下在pandas数据帧中提取最高结果。我不知道该怎么做。我有以下数据框架。我想将df1中的符号与年份匹配且价格最低的df2相关联。我不想超过df1购买的金额。举个例子,我想购买所有x1和75000的x2以在df1中完成我的X顺序。我想创建一个df2中所有符号的列表,它将满足df1中的订单。有什么建议吗?
df1 = pd.DataFrame({'Symbol':list('XYZ'),'Amount to buy':[125000,200000,50000],'Years':[5,7,3]})
Symbol Amount to Buy Years
X 125000 5
Y 200000 7
Z 50000 3
df2 = pd.DataFrame({'Symbol':'X1,X2,Y1,Y2,Z1,Z2'.split(','),'Amount Offered':[50000,100000,165000,110000,85000,90000],'Years':[5,5,7,7,3,3],'Price':'$5,$6,$3,$5,$7,$10'.split(',')})
Symbol Amount Offered Years Price
X1 50000 5 $5
X2 100000 5 $6
Y1 165000 7 $3
Y2 110000 7 $5
Z1 85000 3 $7
Z2 90000 3 $10
for Amount_Offered in df2.iterrows():
if Amount_Offered < x_buy:
print ('buy this')
ValueError: The truth value of a Series is ambiguous. Use a.empty,
a.bool(), a.item(), a.any() or a.all().
编辑:到目前为止,我已将我的X amounnt设置为变量x_buy
答案 0 :(得分:0)
根据我对你的问题的解读,我认为下面的代码会给你你想要的结果 - 但是,我必须警告你,虽然它有效,但这是一个非常难看的解决方案。
df = pd.Series()
for df1_i, df1_row in df1.iterrows():
symbol = df1_row['Symbol']
remaining = df1_row['Amount to Buy']
for df2_i, df2_row in df2.iterrows():
if remaining > 0:
if symbol in df2_row['Symbol']:
remaining -= df2_row['Amount Offered']
df = df.set_value(df2_i, df2_row['Symbol'])
运行您通过此代码提供的数据会产生Series
,如下所示:
0 X1
1 X2
2 Y1
3 Y2
4 Z1