我有一个很大的实验日志文件 (.txt)(最多包含 100 000 个条目),其结构如下:
ROUTINE TEMPERATURE VOLTAGE WAVELENGTH
_______________________________________________
CHANGE T 75 0 560
CHANGE T 80 0 560
CHANGE T 85 0 560
CHANGE T 90 0 560
OSL 75 20 570
OSL 75 20 580
OSL 75 20 590
OSL 75 20 600
CHANGE T 75 0 560
CHANGE T 80 0 560
CHANGE T 85 0 560
CHANGE T 90 0 560
我使用 pandas 的 read_table 将日志文件加载到 python 中。我想根据第一列的值将生成的数据帧分成更小的数据帧。所以结果看起来像这样:
**DATAFRAME 1:**
CHANGE T 75 0 560
CHANGE T 80 0 560
CHANGE T 85 0 560
CHANGE T 90 0 560
**DATAFRAME 2:**
OSL 75 20 570
OSL 75 20 580
OSL 75 20 590
OSL 75 20 600
**DATAFRAME 3:**
CHANGE T 75 0 560
CHANGE T 80 0 560
CHANGE T 85 0 560
CHANGE T 90 0 560
首先,我尝试使用第一列值发生变化的索引拆分它们:
indexSplit = [] # list containing the boundry indices
prevRoutine = log['ROUTINE'][0] # log is the complete dataframe
i = 1
while i < len(log):
if prevRoutine != log['ROUTINE'][i]:
indexSplit.append(i)
prevRoutine = log['ROUTINE'][i]
然而,考虑到日志文件的大小,以这种方式(显然)需要花费大量时间。我想知道是否有一种优雅的方法可以用 Pandas 做到这一点?我一直遇到的问题是第一列的值在多个系列中使用。我总是以 dataframe 1 和 dataframe 3 作为一个结束。
您可以使用 list comprehension
,其中循环 groupby
对象和 groups
由 s
创建。比较 ne
(与 !=
相同,但速度更快)shift
编辑专栏和 cumsum
获取输出:
s = df['ROUTINE'].ne(df['ROUTINE'].shift()).cumsum()
print (s)
0 1
1 1
2 1
3 1
4 2
5 2
6 2
7 2
8 3
9 3
10 3
11 3
Name: ROUTINE, dtype: int32
dfs = [g for i,g in df.groupby(df['ROUTINE'].ne(df['ROUTINE'].shift()).cumsum())]
print (dfs)
[ ROUTINE TEMPERATURE VOLTAGE WAVELENGTH
0 CHANGE T 75 0 560
1 CHANGE T 80 0 560
2 CHANGE T 85 0 560
3 CHANGE T 90 0 560, ROUTINE TEMPERATURE VOLTAGE WAVELENGTH
4 OSL 75 20 570
5 OSL 75 20 580
6 OSL 75 20 590
7 OSL 75 20 600, ROUTINE TEMPERATURE VOLTAGE WAVELENGTH
8 CHANGE T 75 0 560
9 CHANGE T 80 0 560
10 CHANGE T 85 0 560
11 CHANGE T 90 0 560]
print (dfs[0])
ROUTINE TEMPERATURE VOLTAGE WAVELENGTH
0 CHANGE T 75 0 560
1 CHANGE T 80 0 560
2 CHANGE T 85 0 560
3 CHANGE T 90 0 560
print (dfs[1])
ROUTINE TEMPERATURE VOLTAGE WAVELENGTH
4 OSL 75 20 570
5 OSL 75 20 580
6 OSL 75 20 590
7 OSL 75 20 600
print (dfs[2])
ROUTINE TEMPERATURE VOLTAGE WAVELENGTH
8 CHANGE T 75 0 560
9 CHANGE T 80 0 560
10 CHANGE T 85 0 560
11 CHANGE T 90 0 560
解决方案很复杂,因为如果对第一列使用 groupby
只能得到 2 组:
dfs = [g for i,g in df.groupby('ROUTINE')]
print (dfs)
[ ROUTINE TEMPERATURE VOLTAGE WAVELENGTH
0 CHANGE T 75 0 560
1 CHANGE T 80 0 560
2 CHANGE T 85 0 560
3 CHANGE T 90 0 560
8 CHANGE T 75 0 560
9 CHANGE T 80 0 560
10 CHANGE T 85 0 560
11 CHANGE T 90 0 560, ROUTINE TEMPERATURE VOLTAGE WAVELENGTH
4 OSL 75 20 570
5 OSL 75 20 580
6 OSL 75 20 590
7 OSL 75 20 600]
我是一名优秀的程序员,十分优秀!