我目前正在使用以下代码汇总数字。对于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())
您可能受到
currentGroup
函数的这一部分的严重影响:尝试将其保存到临时变量中,并在
startswith
中使用temp变量。我计划很快将向量化字符串函数添加到pandas中,这样在这些情况下也会有很大的帮助。在相关问题 更多 >
编程相关推荐