我正在努力使用 Pandas Data Frames 从电子表格转换为 Python。
我有一些原始数据:
Date Temperature
12/4/2003 100
12/5/2003 101
12/8/2003 100
12/9/2003 102
12/10/2003 101
12/11/2003 100
12/12/2003 99
12/15/2003 98
12/16/2003 97
12/17/2003 96
12/18/2003 95
12/19/2003 96
12/22/2003 97
12/23/2003 98
12/24/2003 99
12/26/2003 100
12/29/2003 101
在电子表格中,我正在跟踪基于 %monitor 的趋势。将其视为滚动平均值,但基于百分比。
电子表格的输出:
date temp monitor trend change_in_trend
12/4/2003 100 97.00 warming false
12/5/2003 101 97.97 warming false
12/8/2003 100 97.97 warming false
12/9/2003 102 98.94 warming false
12/10/2003 101 98.94 warming false
12/11/2003 100 98.94 warming false
12/12/2003 99 98.94 warming false
12/15/2003 98 98.94 cooling true
12/16/2003 97 98.94 cooling false
12/17/2003 96 98.88 cooling false
12/18/2003 95 97.85 cooling false
12/19/2003 96 97.85 cooling false
12/22/2003 97 97.85 cooling false
12/23/2003 98 97.85 warming true
12/24/2003 99 97.85 warming false
12/26/2003 100 97.85 warming false
12/29/2003 101 97.97 warming false
假设:
percent_monitor = .03
warming_factor = 1 - percent_monitor
cooling_factor = 1 + percent_monitor
在我的电子表格中,我将第一行的列设置为:
monitor = temp * warming_factor
trending = warming
change_in_trend = false
所有剩余的行都是根据当前行和上一行的列值派生的。
监控列逻辑:
if temp > prev_monitor:
if temp > prev_temp:
if temp * warming_factor > prev_monitor:
monitor = temp*warming_factor
else:
monitor = prev_monitor
else:
monitor = prev_monitor
else:
if temp < prev_monitor:
if temp * cooling_factor < prev_monitor:
monitor = temp * cooling_factor
else:
monitor = prev_monitor
else:
monitor = prev_monitor
趋势栏逻辑:
if temp > prev_monitor:
trending = warming
else:
trending = cooling
趋势栏逻辑的变化:
if current_trend - previous_trend:
change_in_trend = false
else:
change in trend = true
我能够遍历数据框并毫无问题地应用逻辑。但是,在处理数千行时性能非常糟糕。
我一直在尝试以更像“ Pandas ”的方式来做到这一点,但每次尝试都失败了。
如果不粘贴我的代码尝试让自己感到尴尬,有没有人可以为我提供一些帮助?
提前致谢!
请您参考如下方法:
因为您只想将其转移到 Python 而不是特别设置 Pandas,所以我选择了非 Pandas 方法。我使用了您的示例行并在 0.182 秒内完成了 47124 行。
Pandas 对于某些用例来说确实非常好且直观,但迭代速度可能会非常慢。 This page解释了 Pandas 的一些较慢的使用,其中之一主要是索引迭代。一个 pandas-ey 的方法是利用 5。使用 NumPy 数组进行矢量化,但您的用例似乎足够简单,以至于这可能有点矫枉过正,可能不值得(假设您的名字是 PythonNoob)。
为了清晰和速度,简单地使用更基本的 python 函数可以获得您想要的速度。
首先我设置常量
percent_monitor = .03
warming_factor = 1 - percent_monitor
cooling_factor = 1 + percent_monitor
然后(为了便于使用,有更简洁的方法来做到这一点,但这很清楚)我设置与列值对应的列的名称:
DATE = 0
TEMP = 1
MONITOR = 2
TRENDING = 3
CHANGE_IN_TREND = 4
然后我在它自己的函数中提取了你的监视器代码(并稍微清理了 if 语句:
def calculate_monitor(prev_monitor, current_temp, prev_temp):
if (current_temp > prev_monitor) and (current_temp > prev_temp) and (current_temp * warming_factor) > prev_monitor:
return current_temp * warming_factor
elif (current_temp < prev_monitor) and ((current_temp * cooling_factor) < prev_monitor):
return current_temp * cooling_factor
else:
return prev_monitor
最后读入代码并处理:
data = [] # I am going to append everything to this
with open('weather_data.csv') as csv_file:
previous_row = None
csv_reader = csv.reader(csv_file, delimiter=' ')
line_count = 0
for row in csv_reader:
cleaned_row = list(filter(None, row))
if line_count == 0:
# first row is column -- I am leaving it blank you can do whatever you want with it
line_count += 1
elif line_count == 1: # this is the first line
previous_row = cleaned_row + [float(cleaned_row[TEMP]) * warming_factor, "warming", False]
data.append(previous_row)
line_count += 1
else:
monitor = calculate_monitor(float(previous_row[MONITOR]), float(cleaned_row[TEMP]), float(previous_row[TEMP]))
current_trend = 'warming' if float(cleaned_row[TEMP]) > float(previous_row[MONITOR]) else 'cooling'
change_in_trend = False if current_trend != previous_row[CHANGE_IN_TREND] else True
previous_row = cleaned_row + [monitor, current_trend, change_in_trend]
data.append(previous_row)
line_count += 1
这将为您提供所需的速度。如果你想在最后将其转换为 Pandas 数据框,你可以这样做:
df = pd.DataFrame(data, columns=['date', 'temp', 'monitor', 'current_trend', 'change_in_trend'])
