Python 使用GroupBy对象上的筛选器筛选出数据帧
数据集: 使用提供的字典,通过执行以下操作,根据通货膨胀调整新的价格:Python 使用GroupBy对象上的筛选器筛选出数据帧,python,pandas,filter,group-by,pandas-groupby,Python,Pandas,Filter,Group By,Pandas Groupby,数据集: 使用提供的字典,通过执行以下操作,根据通货膨胀调整新的价格: 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 R
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.
我的代码如下:
KeyError:“转换”
如果我删除此项:
df=df.分组依据(“年度”)。适用(通货膨胀)
它可以正常工作,但我认为我需要使用这段代码,因为我必须对该函数进行分类。对吗?谁能给我一些建议?提前感谢。您会收到此错误,因为在这里的
df[df.New\u Price.apply(lambda x:x.isnumeric())]行中,set\u index('New\u Price')
列New\u Price
的类型为float
并且isnumeric()
对字符串有效,因此如果您想使用此代码,请通过astype()将其转换为字符串
方法,即df[df.New\u Price.astype(str).apply(lambda x:x.isnumeric())]。设置索引(“New\u Price”)
那么我该怎么办?似乎我需要删除/删除列New_Price中的任何非数字字符。Lol您使用的是pd.to_numeric()
这里df[“New_Price”]=df[“New_Price”]。应用(pd.to_numeric,downcast='float',errors='concurve')
这将把非数值值转换为NaN,然后使用dropna()
方法删除通过to_numeric()
方法转换为NaN的非数值行=pd.to_numeric(df['New_Price',errors='concurve')df=df.dropna(subset=['New_Price'])我试图用它来代替原来的。但新的错误发生了。-->KeyError:“转换”很有效,非常感谢!
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"]])