我想根据a pandas
df
中的两个columns
来确定count
值。在
下面是一个例子:
import pandas as pd
d = ({
'B' : ['08:00:00','John','08:10:00','Gary','08:41:42','John','08:50:00','John', '09:00:00', 'Gary','09:15:00','John','09:21:00','Gary','09:30:00','Gary','09:40:00','Gary'],
'C' : ['1','1','1','1','1','1','2','2','2', '2','2','2','3','3','3', '3','3','3'],
'A' : ['Stop','','Res','','Start','','Stop','','Res','','Start','','Stop','','Res','','Start','']
})
df = pd.DataFrame(data=d)
输出:
^{pr2}$如果我执行基于Column A
和C
的计数,则返回以下内容:
k = df.groupby('A').C.nunique()
Res 3
Start 3
Stop 3
我希望根据Column B
中的人来划分这些。因此,预期输出将是:
John Stop 2
Res 0 #Nan
Start 2
Gary Stop 1
Res 3
Start 1
我试过k = df.groupby('A').B.C.nunique()
我们可以创建一个扁平的DF:
一个多索引将包括:
^{pr2}$按两列分组:
这是一个奇怪的数据帧,强烈建议不要在同一列中包含时间和名称。再加一列就行了!这会让事情变得简单。在
鉴于您的数据,如果您不介意从John中丢失
RES
:相关问题 更多 >
编程相关推荐