在dataframe列中查找以string开头的所有元素

2024-05-16 03:40:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在使用以下代码汇总数字。对于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())

Tags: id元素dategroupsecstartlistdaily
1条回答
网友
1楼 · 发布于 2024-05-16 03:40:36

您可能受到currentGroup函数的这一部分的严重影响:

attributionCalcDF['GROUP_LIST'].ix[idx]

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

相关问题 更多 >