python-3.x - 如何根据其他列查找列值是否被低估或高估?

标签 python-3.x pandas dataframe

所以我正在尝试解决这个 pandas 练习。我从 Kaggle 得到了这个房地产公司的数据集,数据框 df 看起来像这样。

           id           location       type     price
0       44525        Golden Mile      House   4400000
1       44859           Nagüeles      House   2400000
2       45465           Nagüeles      House   1900000
3       50685           Nagüeles       Plot   4250000
4      130728        Golden Mile      House  32000000
5      130856           Nagüeles       Plot   2900000
6      130857        Golden Mile      House   3900000
7      130897        Golden Mile      House   3148000
8      3484102           Marinha       Plot    478000
9      3484124           Marinha       Plot   2200000
10     3485461           Marinha      House   1980000

所以现在,我必须根据 locationtype 列找出哪些属性被低估或高估,哪些属性具有真实价格。所需的结果应如下所示:

       id           location       type     price   Over_val   Under_val    Norm_val
0   44525        Golden Mile      House   4400000         0      0             1
1   44859           Nagüeles      House   2400000         0      0             1
2   45465           Nagüeles      House   1900000         0      0             1
3   50685           Nagüeles       Plot   4250000         0      1             0
4  130728        Golden Mile      House  32000000         1      0             0
5  130856           Nagüeles       Plot   2900000         0      1             0
6  130857        Golden Mile      House   3900000         0      0             1
7  130897        Golden Mile      House   3148000         0      0             1
8  3484102           Marinha       Plot    478000         0      0             1
9  3484124           Marinha       Plot   2200000         0      0             1
10 3485461           Marinha      House   1980000         0      1             0

卡在上面有一段时间了。我应该尝试什么逻辑来解决这个问题?

最佳答案

这是我的解决方案。作为内联注释包含的解释。可能有一些方法可以用更少的步骤来做到这一点;我也会有兴趣学习的。

import pandas as pd

# Replace this with whatever you have to load your data. This is set up for a sample data file I used
df = pd.read_csv('my_sample_data.csv', encoding='latin-1')

# Mean by location - type
mdf = df.set_index('id').groupby(['location','type'])['price'].mean().rename('mean').to_frame().reset_index()
# StdDev by location - type
sdf = df.set_index('id').groupby(['location','type'])['price'].std().rename('sd').to_frame().reset_index()
# Merge back into the original dataframe
df = df.set_index(['location','type']).join(mdf.set_index(['location','type'])).reset_index()
df = df.set_index(['location','type']).join(sdf.set_index(['location','type'])).reset_index()

# Add the indicator columns
df['Over_val'] = 0
df['Under_val'] = 0
df['Normal_val'] = 0

# Update the indicators
df.loc[df['price'] > df['mean'] + 2 * df['sd'], 'Over_val'] = 1
df.loc[df['price'] < df['mean'] - 2 * df['sd'], 'Under_val'] = 1

df['Normal_val'] = df['Over_val'] + df['Under_val']
df['Normal_val'] = df['Normal_val'].apply(lambda x: 1 if x == 0 else 0)

关于python-3.x - 如何根据其他列查找列值是否被低估或高估?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54469791/

相关文章:

python - 如何连接两个pandas数据框

python - 在 Pandas DataFrame Python 中分组

python - 存储在字典中时评估生成字符串的语句 'later'

Python Bokeh 将绘图的一部分作为链接

python - 修饰 Python3 函数以忽略 (a, b, *args, **kwargs) 中的 *args?

python - 将 Pandas DataFrame 与不同列中的键合并

r - 根据多个先前行/列中的值删除数据框中的行

python-3.x - 带有 Power BI 的 Windows 中的 Anaconda

python - 从满足两个条件的 Dataframe 中获取索引和列

python - 在Python中迭代二维数组?