【问题标题】:How to create Data Frames and call them using an array and for loop?如何创建数据框并使用数组和 for 循环调用它们?
【发布时间】:2020-05-07 15:19:58
【问题描述】:

我的问题如下:

我正在尝试创建一个 for 循环,使用第一个 df 列“大陆”中的大陆名称将单个数据集划分为 6 个数据集(每个大陆一个)

所以首先我取各大洲的名称并将它们存储在一个数组中:

continent = df['continent'].unique()

我创建了 for 循环(我希望每个数据集都被称为一个大陆,并且只有涉及该大陆的行):

for x in continent : 
    x = pd.DataFrame()
    x = df.loc[df["continent"]==x]

有人可以帮我解决这个问题吗?

谢谢,

【问题讨论】:

    标签: python pandas loops for-loop


    【解决方案1】:

    也就是groupby,注意我们把所有的df都存储在dict,你可以通过d['continent name']得到单个df

    d={ x : y for x , y in df.groupby('continent')}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-25
      • 1970-01-01
      • 2021-08-15
      • 1970-01-01
      • 1970-01-01
      • 2021-01-28
      • 2014-10-29
      • 2017-06-16
      相关资源
      最近更新 更多