使用 GroupBy 对象上的过滤器过滤掉数据帧

时间:2021-05-14 05:49:21

标签: python pandas filter group-by pandas-groupby

<块引用>

数据集:https://dl.dropboxusercontent.com/s/v9gmgxupkypn5dw/train-data.csv

conversion_table = {
    1998: 3.7327,
    1999: 3.2372,
    2000: 3.2216,
    2001: 3.1133,
    2002: 2.9603,
    2003: 2.8694,
    2004: 2.7662,
    2005: 2.6652,
    2006: 2.5246,
    2007: 2.3702,
    2008: 2.2461,
    2009: 2.0475,
    2010: 1.7809,
    2011: 1.6270,
    2012: 1.5278,
    2013: 1.3743,
    2014: 1.2594,
    2015: 1.1897,
    2016: 1.1189,
    2017: 1.0945,
    2018: 1.0524,
    2019: 1.0}

使用提供的字典,通过执行以下操作来调整 New_Price 以应对通货膨胀:

     a   Make a function called inflation that inputs a dataframe.

     b   Make a new variable conversion. This is a column with the values of conversion_table that matches the column Year as the key.

     c   Remove any non-numerical characters in the column New_Price. Replace the New_Price with that change.

     d   Convert the column type New_Price into float. Replace the New_Price with that change.

     e   Multiply New_Price with conversion. Replace the New_Price with that change.

     f   Return the dataframe.
     
     Then, Print Year, New_Price, and New_Price_Adjusted.
<块引用>

我的代码如下:

def inflation(df):
  conversion = conversion_table[df["Year"].values[0]]
  df['New_Price'] = pd.to_numeric(df['New_Price'], errors='coerce')
  df = df.dropna(subset=['New_Price'])
  df["New_Price"] = df["New_Price"].apply(pd.to_numeric, downcast='float', errors='coerce')
  df["New_Price"] = df["New_Price"] * df["conversion"]
  return df

df = df.groupby("Year").apply(inflation)
print(df[["Year","New_Price"]])
<块引用>

键错误:'转换'

如果我删除它: df = df.groupby("Year").apply(通货膨胀) 它可以正常工作,但我认为我需要使用此代码,因为我必须对该函数进行分类。对?任何人都可以给我一些想法吗?提前致谢。

0 个答案:

没有答案