我有一个元组..
for i in my_tup:
print(i)
输出:
(Transfer, 0:33:20, Cycle 1)
(Transfer, 0:33:10, Cycle 1)
(Download, 0:09:10, Cycle 1)
(Transfer, 0:33:10, Cycle 1)
(Download, 0:13:00, Cycle 1)
(Download, 0:12:30, Cycle 2)
(Transfer, 0:33:10, Cycle 2)
(Download, 0:02:00, Cycle 2)
(Transfer, 0:33:00, Cycle 2)
(Transfer, 0:33:00, Cycle 2)
(Transfer, 0:33:00, Cycle 2)
(Transfer, 0:32:40, Cycle 2)
我正在尝试计算“转移”PER周期类别的出现次数.即循环1中有多少转移发生,循环2中有多少转移发生等等…
我可以在第一个周期中解决它,但不能在此之后解决这个问题.(实际输出中还有更多的周期).
accumulatedList = []
count = 0
for i in range(0 len(my_tup)):
if my_tup[i][0] == 'Transfer' and my_tup[i][2] == 'Cycle 1':
count +=1
accumulatedList.append(count)
不知道如何为其他人做这件事.
最佳答案 使用pandas库很简单:
import pandas as pd
df = pd.DataFrame(my_tup, columns=['Category', 'TimeSpan', 'Cycle'])
g = df.groupby(['Category', 'Cycle']).size()
它返回:
Category Cycle
Download Cycle 1 2
Cycle 2 2
Transfer Cycle 1 3
Cycle 2 5
dtype: int64
如果您只关心转移,请使用索引对其进行切片:
g['Transfer']
Cycle
Cycle 1 3
Cycle 2 5
dtype: int64