Python matplotlib重叠yaxis
我想为我的数据帧创建一个图表。然而,我得到了这样的东西: 代码: 数据帧: 数据类型: 已更新 我想下面的代码可以工作,但由于我没有数据,我不能完全确定Python matplotlib重叠yaxis,python,python-3.x,jupyter-notebook,Python,Python 3.x,Jupyter Notebook,我想为我的数据帧创建一个图表。然而,我得到了这样的东西: 代码: 数据帧: 数据类型: 已更新 我想下面的代码可以工作,但由于我没有数据,我不能完全确定 def Draw_RoomType_vs_Price(): plt.figure(figsize=(9, 6)) plt.scatter(x=df['room_type'], y=df['price'].apply(lambda x: float(x.replace('$', '')))) plt.title('R
def Draw_RoomType_vs_Price():
plt.figure(figsize=(9, 6))
plt.scatter(x=df['room_type'], y=df['price'].apply(lambda x: float(x.replace('$', ''))))
plt.title('Room Type vs Price', size=15, weight='bold')
plt.xlabel('Room Type', size=12)
plt.ylabel('Price', size=12)
plt.tight_layout()
plt.show()
=============================================
df['price']类型似乎是string而不是int/float。您需要首先使用以下正则表达式删除除数字以外的其他字符:
import re
re.sub("[^0-9]", "", "$102")
然后将结果转换为int或float,您就可以绘制它了。实际上
pandas
中更好的选择是通过dtype
或converters
输入将其强制为float。这样,您就不需要对re
执行regex,因此,我尝试使用pandas将我的数据类型转换为int/float。但是我得到了错误ValueError:无法将字符串转换为float:“$100.00”
请放入数据帧的一部分,以便我可以修改您的代码谢谢您回答我的问题,我已将其修复。我只删除了“$”,但忘了删除“,”。这就是我出现这种错误的原因。除了price
的数据类型问题之外,您正在处理信息的密度分布。因此,您确实应该使用直方图绘制。
import re
re.sub("[^0-9]", "", "$102")