【问题标题】:time taken by code is an issue for python dataframe代码花费的时间是python数据框的一个问题
【发布时间】:2017-02-15 00:43:52
【问题描述】:

我需要有关以下代码所花费的数据帧相关时间的帮助。 完成大约 2000 条记录的数据集大约需要 20 秒。

def findRe(leaddatadf, keyAttributes, datadf):
    for combs in itertools.combinations(atrList,
        len(atrList)-1):

        v_by =(set(atrList) - set(combs)) # varrying


    grpdatapf=datadf.groupby(combs)
    for name, group in grpdatapf:

        if(group.shape[0]>1):

            tmpgdf = leaddatadf[leaddatadf['unique_id'].astype(float).\
                isin(group['unique_id'].astype(float))]
            if(tmpgdf.shape[0]>1):

                tmpgdf['mprice']=tmpgdf['mprice'].astype(float)
                tmpgdf=tmpgdf.sort('mprice')

                tmpgdf['id'] = tmpgdf['id']
                tmpgdf['desc'] = tmpgdf['description']
                tmpgdf['related_id'] = tmpgdf['id'].shift(-1)
                tmpgdf['related_desc'] = tmpgdf['description'].shift(-1)
                tmpgdf['related_mprice'] = tmpgdf['mprice'].shift(-1)

                tmpgdf['pld'] = np.where(
                    (tmpgdf['related_price'].astype(float) > \
                        tmpgdf['mprice'].astype(float)),
                    (tmpgdf['related_price'].astype(float) - \
                        tmpgdf['mprice'].astype(float)) ,
                    (tmpgdf['mprice'].astype(float) - \
                        tmpgdf['related_mprice'].astype(float)))
                tmpgdf['pltxt'] = np.where(
                    tmpgdf['related_mprice'].astype(float) - \
                        tmpgdf['mprice'].astype(float)>0.0,'<',
                    np.where(tmpgdf['related_mprice'].astype(float)\
                        - tmpgdf['mprice'].astype(float)<0,'>','='))
                tmpgdf['prc_rlt_dif_nbr_p'] = abs(
                    (tmpgdf['pld'].astype(float) / \
                        ((tmpgdf['mprice'].astype(float)))) )
                tmpgdf['keyatr'] = str(atrList)
                tmpgdf['varying'] = np.where(1==1,
                    "".join(v_by ),'')# varrying

                temp = tmpgdf[['id',
            'desc', 'related_id',
            'related_desc', 'pltxt', 'pld',
            'prc_rlt_dif_nbr_p', 'mprice', 'related_mprice',
            'keyatr', 'varying']]

                temp = temp[temp['related_mprice'].astype(float)>=0.0]
                reldf.extend(list(temp.T.to_dict().values()))
    return pd.DataFrame(
                reldf, columns = ['id',
                    'desc', 'related_id',
                    'related_desc', 'pltxt', 'pld',
                    'prc_rlt_dif_nbr_p', 'mprice', 'related_mprice',
                    'keyatr', 'varying'])

【问题讨论】:

  • 我认为 Stackoverflow 并不是一个代码审查网站。这个问题并没有特别问什么,所以我认为它应该移到其他地方。
  • 那我应该去哪里问呢

标签: python python-3.x pandas dataframe


【解决方案1】:

请在每行之后打印需要多少毫秒

使用这个https://stackoverflow.com/a/1557584/2655092

并返回花费最多时间的行

【讨论】:

  • 拍摄tmpdf的时间 - 0.00083160400390625 - 由reset_index拍摄的时间 - 0.0006613731384277344 - 由mprice浮动的时间 - 0.0002810955047607422 - 按时间采取的时间由mprice排序 - 0.0015559196472167969 - desc 花费的时间 - 0.0017049312591552734 -
  • 由related_id采取时间 - 0.0018208026885986328 - 时间采取related_desc - 0.0018434524536132812 - 时间采取related_mprice - 0.0015764236450195312 - 时间采取的PLD - 0.0020411014556884766 - 时间采取pltxt - 0.0022830963134765625 - 时间采取prc_rlt_dif_nbr_p - 0.001756429672241211 - keyatr 所用的时间 - 0.0015103816986083984 - 变化所用的时间 - 0.00200653076171875 - allcomb 所用的时间 - 0.0007736682891845703 - 将 df 转换为 dict 列表所用的时间 - 0.00047779083251953125 -
  • 嗨,Jm,我增加了每行所用的时间。总共有 14 个关键 atr 列表大小
【解决方案2】:

您经常使用astype(float)。每次您使用它时,都会创建该系列的副本。当您尝试加载数据框时,您可以尝试在最开始时设置 dtype=float - 这样您只需将系列转换为浮动一次 - 而不是在每次迭代时 :)

如果这有帮助,请告诉我

【讨论】:

  • 谢谢先生,我会这样做,其他我想改进的事情,我有 14-1 个独特的组合,我对每个组合进行分组并查找当前行和下一行之间的关系。
猜你喜欢
  • 1970-01-01
  • 2018-05-22
  • 2014-01-13
  • 2020-08-06
  • 1970-01-01
  • 1970-01-01
  • 2020-08-12
  • 2012-03-25
  • 1970-01-01
相关资源
最近更新 更多