python – Pandas:从另一个数据帧中逐列乘?

前端之家收集整理的这篇文章主要介绍了python – Pandas:从另一个数据帧中逐列乘?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我有两个数据框,都由一个名为month的日期列索引.第一个是df1,有八行.我关心的列是df [‘num_percent’],它看起来像这样:

  1. 2015-02-01 0.071549
  2. 2015-03-01 0.070368
  3. 2015-04-01 0.069291
  4. 2015-05-01 0.068394
  5. 2015-06-01 0.067452
  6. 2015-07-01 0.066302
  7. 2015-08-01 0.065543
  8. 2015-09-01 0.064591
  9. Name: num_percent,dtype: float64

第二个数据帧有100,000行.我关心的专栏是df2 [‘total_quantity’],它的样本如下所示:

  1. 2014-11-01 324199
  2. 2014-12-01 378443
  3. 2015-01-01 367379
  4. 2015-02-01 336863
  5. 2015-03-01 380268
  6. 2015-04-01 386292
  7. 2015-05-01 373213
  8. 2015-06-01 403343
  9. 2015-07-01 414310
  10. 2015-08-01 403684
  11. 2015-09-01 420922
  12. Name: total_quantity,dtype: int64

我想在df2中添加一个新列,它是df2 [‘total_quantity’]的值乘以df1中月份的相应值.

我怎样才能做到这一点?

如果我尝试:

  1. df2['percent'] = df2['total_quantity'] * df1['num_percent']

我得到ValueError:无法从重复的轴重新索引.

更新:这里有一些数据和代码来复制问题:

  1. data = {'month': ['2014-01-01','2014-02-01','2014-03-01'],'num_percent': [0.4,0.5,0.6]}
  2. df1 = pd.DataFrame(data)
  3. df1['month'] = pd.to_datetime(df1['month'])
  4. df1 = df1.set_index('month')
  5. data = {'month': ['2014-01-01','2014-03-01','2014-01-01'],'org': ['00K','00K','00L'],'total_quantity': [1000,1000,2000,1000]}
  6. df2 = pd.DataFrame(data)
  7. df2['month'] = pd.to_datetime(df2['month'])
  8. df2 = df2.set_index('month')
  9. # Both of these produce ValueError: cannot reindex...
  10. df2['percent'] = df1['num_percent'] * df2['total_quantity']
  11. df2.loc[df2.index.isin(df1.index),'percent'] = df2['total_quantity'] * df1['num_percent']
最佳答案
如果您首先是join dfs,那么您可以乘以:

  1. In [24]:
  2. df3 = df1.join(df2)
  3. df3['percent'] = df3['num_percent'] * df3['total_quantity']
  4. df3
  5. Out[24]:
  6. num_percent org total_quantity percent
  7. month
  8. 2014-01-01 0.4 00K 1000 400
  9. 2014-01-01 0.4 00L 1000 400
  10. 2014-02-01 0.5 00K 1000 500
  11. 2014-03-01 0.6 00K 2000 1200

猜你在找的Python相关文章