Hello I have the following dataframe.
您好我有以下数据帧。
Group Size
Short Small
Short Small
Moderate Medium
Moderate Small
Tall Large
I want to count the frequency of how many time the same row appears in the dataframe.
我想计算同一行在数据帧中出现的时间的频率。
Group Size Time
Short Small 2
Moderate Medium 1
Moderate Small 1
Tall Large 1
2 个解决方案
#1
49
You can use groupby's size
:
你可以使用groupby的大小:
In [11]: df.groupby(["Group", "Size"]).size()
Out[11]:
Group Size
Moderate Medium 1
Small 1
Short Small 2
Tall Large 1
dtype: int64
In [12]: df.groupby(["Group", "Size"]).size().reset_index(name="Time")
Out[12]:
Group Size Time
0 Moderate Medium 1
1 Moderate Small 1
2 Short Small 2
3 Tall Large 1
#2
16
You can also try pd.crosstab()
你也可以试试pd.crosstab()
Group Size
Short Small
Short Small
Moderate Medium
Moderate Small
Tall Large
pd.crosstab(df.Group,df.Size)
Size Large Medium Small
Group
Moderate 0 1 1
Short 0 0 2
Tall 1 0 0
EDIT: In order to get your out put
编辑:为了让你的出局
pd.crosstab(df.Group,df.Size).replace(0,np.nan).\
stack().reset_index().rename(columns={0:'Time'})
Out[591]:
Group Size Time
0 Moderate Medium 1.0
1 Moderate Small 1.0
2 Short Small 2.0
3 Tall Large 1.0
#1
49
You can use groupby's size
:
你可以使用groupby的大小:
In [11]: df.groupby(["Group", "Size"]).size()
Out[11]:
Group Size
Moderate Medium 1
Small 1
Short Small 2
Tall Large 1
dtype: int64
In [12]: df.groupby(["Group", "Size"]).size().reset_index(name="Time")
Out[12]:
Group Size Time
0 Moderate Medium 1
1 Moderate Small 1
2 Short Small 2
3 Tall Large 1
#2
16
You can also try pd.crosstab()
你也可以试试pd.crosstab()
Group Size
Short Small
Short Small
Moderate Medium
Moderate Small
Tall Large
pd.crosstab(df.Group,df.Size)
Size Large Medium Small
Group
Moderate 0 1 1
Short 0 0 2
Tall 1 0 0
EDIT: In order to get your out put
编辑:为了让你的出局
pd.crosstab(df.Group,df.Size).replace(0,np.nan).\
stack().reset_index().rename(columns={0:'Time'})
Out[591]:
Group Size Time
0 Moderate Medium 1.0
1 Moderate Small 1.0
2 Short Small 2.0
3 Tall Large 1.0