首页 > 解决方案 > 防止在单个单元格python中滚动

问题描述

for feature in features_with_na:
data = train.copy()

# make a variable that indicates 1 if the observation was missing or 0 if not missing
data[feature] = np.where(data[feature].isnull(), 1, 0)

# calculate median sales price where the information is missing or present
data.groupby(feature)['saleprice'].median().plot.bar()
plt.title(feature)
plt.show()

我运行此代码是为了查看我的 pandas 数据框中的各个列之间的关系以及销售价格(恰好是其中一列)。我想通过将我的缺失值转换为 1 来寻找销售价格与列中缺失值之间的某种形式的关系,如果它不是缺失值,则为 0。但是,我得到了很多情节,我必须滚动一个单独的单元格,这使得它变得非常困难。是否有代码可以让我防止滚动特定的内容,以便我一次看到所有的图?

标签: pythonpandasmatplotlibdata-science

解决方案


您可以对代码进行一些修改并获得所需的绘图:

说“价格”=“销售价格”。

feature_data_with_na_and_Price = feature_with_na.copy()
feature_data_with_na_and_Price.append('Price')
data = train[feature_data_with_na_and_Price]

features = []  # store all the features with missing value
null_medians = []  # will store all the price where there was missing values
not_null_medians = []   # will store all the price where there was no missing values
for feature in feature_with_na:

    # make a variable that indicates 1 if the observation was missing or 0 if not missing
    data[feature] = np.where(data[feature].isnull(), 1, 0)

    # the median price for null values
    null_median = data[data[feature] == 1]['Price'].median()

    # the median price for not null values
    not_null_median = data[data[feature] == 0]['Price'].median()

    # append the median price where there is missing values in null_medians
    null_medians.append(null_median)

    # append the median price where there is no missing values in not_null_medians
    not_null_medians.append(not_null_median)

    # append the feature
    features.append(feature)

# create two different dataframes for missing values and not missing values
df1 = pd.DataFrame({'feature': features, 'Price': null_medians})
df2 = pd.DataFrame({'feature': features, 'Price': not_null_medians})

# Add hue
df1['hue']=1   # 1 means missing values
df2['hue']=0   # 0 means no missing values

# concatenate the two dataframes
df = pd.concat([df1, df2])

import seaborn as sns
plt.figure(figsize=(20,10))
sns.barplot(x='feature', y = 'Price', data = df, hue='hue')
plt.xticks(rotation=90, ha='right')

推荐阅读