【发布时间】:2021-04-02 18:47:12
【问题描述】:
Dataframe 中的一列是STANME(州名)。我想用 index = STNAME 和 value = DataFrame 中的条目数创建一个熊猫系列。示例输出如下所示
STNAME
Michigan 83
Arizona 15
Wisconsin 72
Montana 56
North Carolina 100
Utah 29
New Jersey 21
Wyoming 23
我目前的解决方案如下,但由于需要选择任意列、重命名此列等,看起来有点笨拙。想知道是否有更好的方法来做到这一点
grouped=df.groupby('STNAME')
# Note: County is an arbitrary column name I picked from the dataframe
grouped_df = grouped['COUNTY'].agg(np.size)
grouped_df.columns = ['Num Counties']
【问题讨论】:
标签: python pandas dataframe pandas-groupby