使用seaborn绘图时转换pandas列的数据类型

我试图使用seaborn的regplot函数来绘制数据集中价格与每百公里油耗(km/100L)的关系图。我尝试将这两个列的数据类型转换为int64,但没有成功。

automobile_df["price"].astype("int64")automobile_df["km/100L"].astype("int64")

然后我尝试使用seaborn库的regplot函数来绘制价格与每百公里油耗(km/100L)的关系图。

sns.regplot(x="km/100L",y="price",data="automobile_df")

我得到的完整错误信息是,

    TypeError                                 Traceback (most recent call last)<ipython-input-53-fdf8be478666> in <module>()----> 1 sns.regplot(x="km/100L",y="price",data="temp_df")/usr/local/lib/python3.6/dist-packages/seaborn/regression.py in regplot(x, y, data, x_estimator, x_bins, x_ci, scatter, fit_reg, ci, n_boot, units, seed, order, logistic, lowess, robust, logx, x_partial, y_partial, truncate, dropna, x_jitter, y_jitter, label, color, marker, scatter_kws, line_kws, ax)    807                                  order, logistic, lowess, robust, logx,    808                                  x_partial, y_partial, truncate, dropna,--> 809                                  x_jitter, y_jitter, color, label)    810     811     if ax is None:/usr/local/lib/python3.6/dist-packages/seaborn/regression.py in __init__(self, x, y, data, x_estimator, x_bins, x_ci, scatter, fit_reg, ci, n_boot, units, seed, order, logistic, lowess, robust, logx, x_partial, y_partial, truncate, dropna, x_jitter, y_jitter, color, label)    107         # Extract the data vals from the arguments or passed dataframe    108         self.establish_variables(data, x=x, y=y, units=units,--> 109                                  x_partial=x_partial, y_partial=y_partial)    110     111         # Drop null observations/usr/local/lib/python3.6/dist-packages/seaborn/regression.py in establish_variables(self, data, **kws)     43         for var, val in kws.items():     44             if isinstance(val, str):---> 45                 vector = data[val]     46             elif isinstance(val, list):     47                 vector = np.asarray(val)TypeError: string indices must be integers

回答:

我认为问题的原因是astype()默认返回一个副本(参见文档),所以你需要将结果赋值给同一个列,像这样:

automobile_df["price"] = automobile_df["price"].astype("int64")automobile_df["km/100L"] = automobile_df["km/100L"].astype("int64")

或者如果你只想在绘图时更改类型,可以这样使用

sns.regplot(x=automobile_df["km/100L"].astype("int64"),y=automobile_df["price"].astype("int64"))

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注