python - 使用 GroupBy 对象上的过滤器过滤出 DataFrame
问题描述
数据集: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"]])
KeyError:“转换”
如果我删除它: df = df.groupby("Year").apply(inflation) 它可以正常工作,但我认为我需要使用此代码,因为我必须对该函数进行分类。正确的?任何人都可以给我一些想法吗?提前致谢。
解决方案
推荐阅读
- spring - how to stop default query on id when using @cacheable annotation
- reactjs - React redux props undefined
- javascript - Javascript prevent decrement past 0
- python - 您如何处理在 Python 中生成许多变量或对象的任务?
- c++ - 什么是“#include
“? - python-3.x - 在特定小时之间每 10 秒运行一次 bash 脚本
- c# - Handle Linq queries outputs
- python - 在python中使用for循环时如何更新列表
- javascript - 动态创建的字段/ID 中的 foreach 解决方案
- javascript - 如何让nodejs在继续之前等待mongodb连接(以阻塞方式调用异步)