【问题标题】:Find all elements in dataframe column that startswith string查找以字符串开头的数据框列中的所有元素
【发布时间】:2012-06-30 11:44:57
【问题描述】:

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

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    currentGroup 函数的这一部分可能会让您深受打击:

    attributionCalcDF['GROUP_LIST'].ix[idx]
    

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

    【讨论】:

    • 非常感谢韦斯。将此功能的时间缩短了约 40%。向量化字符串函数?...非常期待!
    猜你喜欢
    • 2023-04-01
    • 2016-12-17
    • 2011-11-30
    • 2011-08-02
    • 2022-10-19
    • 2011-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多