【问题标题】:Prevent scrolling in an individual cell python防止在单个单元格python中滚动
【发布时间】:2020-08-13 05:38:44
【问题描述】:
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。但是,我得到了很多情节,我必须滚动一个单独的单元格,这使得它变得非常困难。是否有代码可以让我防止滚动特定的内容,以便我一次看到所有的图?

【问题讨论】:

    标签: python pandas matplotlib data-science


    【解决方案1】:

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

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

    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')
    

    【讨论】:

      猜你喜欢
      • 2011-08-29
      • 2018-06-16
      • 1970-01-01
      • 2013-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-13
      • 1970-01-01
      相关资源
      最近更新 更多