通过消除三个for循环来提高时间效率

时间:2018-05-12 00:35:23

标签: python python-3.x performance pandas apply

我有一个类似的脚本:

import random
import pandas as pd

FA = []
FB = []
Value = []
df = pd.DataFrame()
df_save = pd.DataFrame(index=['min','max'])
days = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
numbers = list(range(24))   # FA.unique()
mix = '(pairwise combination of days and numbers, i.e. 0Monday,0Tuesday,...1Monday,1Tuesday,....)' 'I dont know how to do this combination btw'

def Calculus():
    global min,max
    min = df['Value'][boolean].min()
    max = df['Value'][boolean].max()

for i in range(1000):
    FA.append(random.randrange(0,23,1))
    FB.append(random.choice(days))
    Value.append(random.random())

df['FA'] = FA
df['FB'] = FB
df['FAB'] = df['FA'].astype(str) + df['FB'].astype(str)
df['Value'] = Value

mix_factor = df['FA'].astype(str) + df['FB'].astype(str)

for i in numbers:
    boolean = df['FA'] == i
    Calculus()
    df_save[str(i)] = [min,max]

for i in days:
    boolean = df['FB'] == i
    Calculus()
    df_save[str(i)] = [min,max]

for i in mix_factor.unique():
    boolean = df['FAB'] == i
    Calculus()  #
    df_save[str(i)] = [min,max]

我的问题是:还有另一种方法可以做同样但更有效的时间吗?我的真实数据(在这种情况下是df)是一个拥有数百万行的csv,这三个循环正在永远。 也许使用'apply'但我之前从未使用过它。

非常感谢任何见解,谢谢。

1 个答案:

答案 0 :(得分:-1)

您可以将所有三个循环放入一个,具体取决于您的确切代码。微积分有参数吗?如果没有,将它们合并为一个将允许你必须运行微积分()少