1. # coding=utf-8
    2. import pandas as pd
    3. import numpy as np
    4. file_path = "./starbucks_store_worldwide.csv"
    5. df = pd.read_csv(file_path)
    6. # print(df.head(1))
    7. # print(df.info())
    8. # grouped = df.groupby(by="Country")
    9. # print(grouped)
    10. #DataFrameGroupBy
    11. #可以进行遍历
    12. # for i,j in grouped:
    13. # print(i)
    14. # print("-"*100)
    15. # print(j,type(j))
    16. # print("*"*100)
    17. # df[df["Country"]="US"]
    18. #调用聚合方法
    19. # country_count = grouped["Brand"].count()
    20. # print(country_count["US"])
    21. # print(country_count["CN"])
    22. #统计中国每个省店铺的数量
    23. # china_data = df[df["Country"] =="CN"]
    24. #
    25. # grouped = china_data.groupby(by="State/Province").count()["Brand"]
    26. #
    27. # print(grouped)
    28. #数据按照多个条件进行分组,返回Series
    29. # grouped = df["Brand"].groupby(by=[df["Country"],df["State/Province"]]).count()
    30. # print(grouped)
    31. # print(type(grouped))
    32. #数据按照多个条件进行分组,返回DataFrame
    33. grouped1 = df[["Brand"]].groupby(by=[df["Country"],df["State/Province"]]).count()
    34. # grouped2= df.groupby(by=[df["Country"],df["State/Province"]])[["Brand"]].count()
    35. # grouped3 = df.groupby(by=[df["Country"],df["State/Province"]]).count()[["Brand"]]
    36. print(grouped1,type(grouped1))
    37. # print("*"*100)
    38. # print(grouped2,type(grouped2))
    39. # print("*"*100)
    40. #
    41. # print(grouped3,type(grouped3))
    42. #索引的方法和属性
    43. print(grouped1.index)