因此,我提取了2个数据帧,如下所示:
DF1:
DF2:
我希望基于另一个数据帧中的条件匹配,将一个因子应用于DF1的pricedata 1和pricedata2列的不同部分
例如,对于DF1中的第0行,我希望在pricedata1值100.5上应用一个因子,方法是根据DF1列年份值==DF2列名称和DF1列名称==DF2 pricename列值的条件乘以从DF2派生的2.5。然后在2007年对pricedata1应用另一个因子5
我知道如何对整个列使用df.apply,但对于如何根据不同的if条件将其部分应用于列,我感到非常困惑
期望输出:
提前谢谢
Tags:
一个简洁的解决方案是^{} 你的
df2
在df1
上。首先重塑df2
以匹配df1
(年份作为行,价格名称作为列),然后reindex()
并按元素乘以比例因子注意:这取决于两个索引具有相同的数据类型,因此根据需要转换
year.astype(...)
您可以通过按行应用
df1
来实现这一点:中波
此代码的工作逻辑如下:
沿着
row in df1
进行迭代,并在df2.iloc[0].iloc[coln.index(j)]
的帮助下更新i-th row
,其中,
coln=列表(df2.列);df2的列,我们将在未来的迭代匹配中使用它。
coln.指数(j);给出j的指数,其中
j
是年.
有用的代码就在这一部分。剩下的我用来从头开始制作
dataframe
:完整代码:
相关问题 更多 >
编程相关推荐