import pandas as pd
# load the sample data
data = {'Group': ['Short', 'Short', 'Moderate', 'Moderate', 'Tall'], 'Size': ['Small', 'Small', 'Medium', 'Small', 'Large']}
df = pd.DataFrame(data)
选项1:
dfg = df.groupby(by=["Group", "Size"]).size()
# which results in a pandas.core.series.Series
Group Size
Moderate Medium 1
Small 1
Short Small 2
Tall Large 1
dtype: int64
备选方案二:
dfg = df.groupby(by=["Group", "Size"]).size().reset_index(name="Time")
# which results in a pandas.core.frame.DataFrame
Group Size Time
0 Moderate Medium 1
1 Moderate Small 1
2 Short Small 2
3 Tall Large 1
备选方案3:
dfg = df.groupby(by=["Group", "Size"], as_index=False).size()
# which results in a pandas.core.frame.DataFrame
Group Size Time
0 Moderate Medium 1
1 Moderate Small 1
2 Short Small 2
3 Tall Large 1
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
编辑:为了让你的输出
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
3条答案
按热度按时间2lpgd9681#
您可以使用groupby的
size
选项1:
备选方案二:
备选方案3:
e4eetjau2#
value_counts
现在接受多列您也可以尝试
pd.crosstab()
编辑:为了让你的输出
bgibtngc3#
其他可能性是使用
.pivot_table()
和aggfunc='size'