【问题标题】:Conditional replacement and nested for loops in pythonpython中的条件替换和嵌套for循环
【发布时间】:2019-04-17 03:22:15
【问题描述】:

我还是 python 新手,不完全确定如何解决这个问题。我有一个关于视频游戏的数据框,其中包含标题、平台、全球销售和重要发布日期。有些条目缺少发布日期。如果条目也具有非 0 全球销售值,我想用平台的平均发布日期替换缺失值。我不完全确定如何构建它以获取适当的平均值,无论我是否需要嵌套循环等。请让我知道我是否走在正确的轨道上,或者我能做些什么来合并它如果您需要任何澄清,谢谢!

    games.head()
                        Name         Platform  Global_Sales Release_Date
    0  Grand Theft Auto: San Andreas      PS2         20.81   2004-10-26
    1             Grand Theft Auto V      PS3         20.30   2013-09-17
    2             Grand Theft Auto V      PS4         18.46   2014-11-18
    3    Grand Theft Auto: Vice City      PS2         16.15   2002-10-28
    4             Grand Theft Auto V     X360         15.85   2013-09-17

    games.info()
    <class 'pandas.core.frame.DataFrame'>
    Int64Index: 28852 entries, 0 to 28851
    Data columns (total 4 columns):
    Name            28852 non-null object
    Platform        28852 non-null category
    Global_Sales    16025 non-null float64
    Release_Date    27757 non-null datetime64[ns]

    for date in games.Release_Date:
      if pd.isnull(date) and games.Global_Sales !=0:
        games.Release_Date = [mean Release_Year for appropriate Platform]

我有另一个带有平均值的 df:platform_means,取自拆分我的日期时间对象并找到我想要使用的平均年份值。

    platform_means.head()
      Platform      Release_Year  
    0      3DS       2012.282895       
    1       DC       2000.077778       
    2       DS       2007.654777       
    3       GB       1999.375000       
    4      GBA       2003.180401       

所以这将是我想要的一个例子,希望它有所帮助。我可以将 Release_Date 用作日期时间或 Release_Date,它是一个 int,取决于哪个更容易。我以前从未有过约会时间。

来自这样的事情:

    games.head()
                                Name         Platform  Global_Sales Release_Date
            0             A                       PS2         20.81   2004-10-26
            1             B                       GBA         20.30   nan
            2             C                       PS4         00.00   nan
            3             D                       PS2           nan   nan
            4             E                      X360         15.85   2013-09-17

到这里:

    games.head()
                                Name         Platform  Global_Sales Release_Date
            0             A                       PS2         20.81   2004-10-26
            1             B                       GBA         20.30   2003.18
            2             C                       PS4         00.00   nan
            3             D                       PS2           nan   nan
            4             E                      X360         15.85   2013-09-17

我一直在使用类似的东西并且它有效,但条件部分没有。使用条件,我得到一个错误,但没有它,我只是替换所有缺少日期的行,而不仅仅是那些也有销售值的行:

    for index, row in games[games['Release_Date'].isnull()].iterrows():
       if games['Global_Sales'] <= 0.01 | games['Global_Sales'].isnull():
           games.loc[games.index == index, 'Release_Date'] = 
    platform_means.loc[platform_means.Platform == row['Platform'], 
    'Release_Year'].item()

【问题讨论】:

  • 请提供我们可以重现的数据框,例如见How to make good reproducible pandas examples。这不仅是为了我们方便,也是为了确保我们推断出正确的数据类型。
  • 您能添加一个具有所需输出的示例吗?你的Release_Datedatetime64[ns] 对象,而Release_Yearfloat64 我假设。你打算如何以datetime64[ns] 格式表示float64 值?
  • 嗨,(抱歉这周很忙)。 Jpp - 即使在阅读帖子后我也不明白具体出了什么问题,你能澄清一下吗? @zipa 当然!我对日期时间不太熟悉,我有另一列只有年份作为整数,我可以使用它(以及我从中获得平均值的地方)或者更容易的那个。我只是在寻找如何提出条件代码的一般格式。

标签: python pandas for-loop if-statement


【解决方案1】:

您可能正在寻找以下内容:

for index, row in games[games['Release_Date'].isnull()].iterrows():
    games.loc[games.index == index, 'Release_Date'] = platform_means.loc[platform_means.Platform == row['Platform'],'Release_Year'].item()

【讨论】:

  • 感谢这项工作,但它会替换所有缺失值,而不仅仅是那些在 global_sales 中也具有非 0/null 值的值。我不确定如何正确地将条件合并到此,有没有简单的方法?
  • 我一直在尝试这样的事情,但它没有像我预期的那样工作,我收到一个错误:一个系列的真值是不明确的。
  • @JasonWilcox 我明天早上 (GMT) 会靠近我的电脑。您是否尝试过过滤 2 个条件,而不仅仅是 « games.loc[(condition1)&(condition2),'Realease_Date'] = ... 中的索引
  • 谢谢!!!是的,我尝试了一些不同的条件,但改变了主意,决定我更愿意替换所有缺失的值,而不是稍后删除不需要的数据(对于某些分析,估算值会有所帮助)。再次感谢您的帮助,我还在学习 Python(更喜欢 R!)
【解决方案2】:

我会尝试使用pd.where 方法。见docs

games['Release_Date'].where(games['Release_Date'].isnull(), 
                            games.join(platform_means, on='Platform')['Release_Year'])

【讨论】:

    猜你喜欢
    • 2020-03-06
    • 1970-01-01
    • 2019-06-06
    • 2016-07-14
    • 2015-04-25
    • 2021-08-09
    • 2020-06-03
    • 2010-10-03
    • 2018-05-21
    相关资源
    最近更新 更多