简体   繁体   English

熊猫将一列的条目除以另一个数据帧中的条目

[英]Pandas divide entries of a column by entries from another data frame

I have 2 dataframes - A and B. A contains weekly sales data for various stores, departments indexed by a key Store_Dept_Date (eg. 2_12_2010-04-03 )while B contains corresponding Consumer Price Index (CPI) for given store and date indexed as Store_Date for eg 2_2010-04-03 . 我有2个数据框-A和B。A包含各商店的每周销售数据,这些部门由键Store_Dept_Date索引(例如2_12_2010-04-03 ),而B包含给定商店的相应消费者价格指数(CPI),索引的日期为Store_Date ,例如2_2010-04-03

> A.columns  
> Out [ ] : Index([u'Store', u'Dept', u'Date', u'Weekly_Sales'], dtype='object')

> B.columns  
> Out [ ] : Index([u'Store', u'Date', u'CPI'], dtype='object')

I want to normalize the weekly sales given in A by dividing each row of A by corresponding CPI value given in B. 我想通过将A的每一行除以B中的相应CPI值来归一化A中的每周销售额。

Currently I am trying this: 目前,我正在尝试:

for ix,row in A.iterrows():
  f_index = str(row['Store']) + "_" + row['Date']
  A.ix[ix,'Weekly_Sales'] = row['Weekly_Sales']/ B.ix[f_index,'CPI']

A contains 421570 rows. A包含421570行。 My program takes forever to run. 我的程序永远运行。 Whats the correct and efficient way of doing it? 什么是正确有效的方法?

The DataFrames' merge method should be faster even though it copies data. 即使DataFrames的merge方法复制数据,也应更快。 You can set the flag copy=False to minimize unnecessary copying. 您可以将标志copy=False为最小化不必要的复制。

If there is one date in B for every date in A, then you can do: 如果B中的每个日期都有一个日期,那么您可以执行以下操作:

C = A.merge(B, on=['Store', 'Date'], copy=False)
C['Normalized_Sales'] = C.Weekly_Sales / C.CPI

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 熊猫将0/1数据框条目映射到列名 - Pandas map 0/1 data frame entries to column names 如何根据来自不同数据帧的列中的条目在数据帧上应用 Pandas 过滤器(无连接) - How to apply a Pandas filter on a data frame based on entries in a column from a different data frame (no join) 如何用 python 中其他数据帧的条目替换一个数据帧中缺少的列条目? - How to replace missing entries of a column in one data frame with entries from other data frame in python? 从Pandas数据框中获取最后条目的最佳方法 - Best way to get last entries from Pandas data frame 从 pandas 数据框中的唯一条目创建字典 - Creating a dictionary from unique entries in a pandas data frame 带有列表条目熊猫数据框的数据透视表 - Pivot table with list entries pandas data frame 根据列中的不同条目过滤 pandas 数据帧(逗号分隔字符串列表) - Filtering pandas data frame based on different entries in a column (list of comma-separated strings) 是否有某种方法可以仅 pandas 数据帧条目在列中具有特定值? - Is there some way to plot only pandas data frame entries with a specific value in a column? 创建一个新列,该列是一行中有多少条目满足pandas中数据帧的每一行条件的计数 - Create new column that is a count of how many entries in a row satisfy a condition for each row of a data frame in pandas 使用 Pandas 保存列中条目的总数 - Saving the total count from entries in a column with Pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM