1

我目前正在使用以下代码汇总数字。对于 中的每个元素dataframe,我设置了一些汇总条件,但它是已创建报告中最慢的部分。是否有更快的方法来识别数据框中以某个字符串开头的所有元素?

for idx, eachRecord in attributionCalcDF.T.iteritems():        
   if (attributionCalcDF['SEC_ID'].ix[idx] == 0):

       currentGroup = lambda x:  str(x).startswith(attributionCalcDF['GROUP_LIST'].ix[idx])
       currentGroupArray = attributionCalcDF['GROUP_LIST'].map(currentGroup)

       attributionCalcDF['ROLLUP_DAILY_TIMING_IMPACT'].ix[idx] = (
                                                         attributionCalcDF['DAILY_TIMING_IMPACT'][(attributionCalcDF['SEC_ID'] != 0) & 
                                                        (currentGroupArray) & 
                                                        (attributionCalcDF['START_DATE'] == attributionCalcDF['START_DATE'].ix[idx])].sum())

       attributionCalcDF['ROLLUP_DAILY_STOCK_TO_GROUP_IMPACT'].ix[idx] = (
                                                         attributionCalcDF['DAILY_STOCK_TO_GROUP_IMPACT'][(attributionCalcDF['SEC_ID'] != 0) & 
                                                        (currentGroupArray) & 
                                                        (attributionCalcDF['START_DATE'] == attributionCalcDF['START_DATE'].ix[idx])].sum())
4

1 回答 1

1

这部分功能可能会给您带来沉重的打击currentGroup

attributionCalcDF['GROUP_LIST'].ix[idx]

尝试将其保存到临时变量并在startswith. 我计划很快为 pandas 添加矢量化字符串函数,这样在这些情况下也会有很大帮助。

于 2012-06-29T14:10:32.430 回答