【问题标题】:What is meant by 'frame' in this for loop? [duplicate]这个 for 循环中的“框架”是什么意思? [复制]
【发布时间】:2018-05-27 23:50:37
【问题描述】:
for group, frame in Top15.groupby(ContinentDict):
    groups.loc[group] = [len(frame), frame['Estimate Population'].sum(),frame['Estimate Population'].mean(),frame['Estimate Population'].std()]

我了解 for 循环作用于每个组,但什么是框架?那是一排吗?柱子?价值?我猜是dataFrame,但是如果循环只作用于dataFrame Top15,怎么会有不止一帧呢?

【问题讨论】:

  • 尝试使用print(frame) 看看会发生什么:)
  • groupby docs 中有一个部分可以准确解决您的问题。您将获得一个单独的 frame 用于分组中的每个级别。

标签: python pandas pandas-groupby


【解决方案1】:

假设我在 df 中加载了以下数据。

continent   count
0   Asia    1
1   Asia    1
2   Asia    1
10  Europe  1
11  Europe  1

for i,j in df[['continent','count']].groupby('continent'):
    print(i,j.shape)

上述查询的结果是

Asia  (3,2)
Europe (2,2)

j 是 df 的切片,其中

j = df.loc[df['continent'] == 'Asia',:] with index as 'continent' 

分别

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-18
    • 2020-09-28
    • 2011-08-06
    • 2019-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多