我试图按“ID”列将我的数据分组。然后我要计算每个ID的“序列”的频率。以下是数据框架的示例:
ID Sequence
101 1-2
101 3-1
101 1-2
102 4-6
102 7-8
102 4-6
102 4-6
103 1118-69
104 1-2
104 1-2
我正在寻找一个与以下相同的计数:
ID Sequence Count
101 1-2 2
3-1 1
102 4-6 3
7-8 1
103 1118-69 1
104 1-2 2
我在python中尝试了这段代码,但它并没有给我想要的
df.groupby('ID')块引号
df.groupby('Sequence').count()
发布于 2020-05-29 21:57:24
这个简单的守则起了作用:
Count_sequence = df.groupby(['ID','Sequence']).count()
用于获取excel表中的输出:
Count_sequence.to_excel('sequence_count.xlsx)
https://datascience.stackexchange.com/questions/75081
复制相似问题