我想为我的数据框创建一个图表。然而,我得到了类似这样的东西:

代码:
def Draw_RoomType_vs_Price():
plt.figure(figsize=(9,6))
plt.scatter(x=df['room_type'], y=df['price'])
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()
Draw_RoomType_vs_Price()数据帧:

数据类型:

发布于 2021-05-03 09:31:30
更新!
我猜下面的代码可以工作,但由于我没有数据,我不能完全确定。
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,您就可以绘制它了。
https://stackoverflow.com/questions/67362297
复制相似问题