将 pandas 数据框转换为数字; seaborn 无法绘图
Converting pandas dataframe to numeric; seaborn can't plot
我正在尝试使用天气数据、pandas 和 seaborn 创建一些图表。不过,我在使用 lmplot(或与此相关的任何其他 seaborn 绘图函数)时遇到了问题。有人告诉我它不能连接 str 和 float 对象,但我事先使用了 convert_objects(convert_numeric=True) ,所以我不确定问题是什么,当我只是打印数据框本身我没有看到任何错误。
import numpy as np
import pandas as pd
import seaborn as sns
new.convert_objects(convert_numeric=True)
sns.lmplot("AvgSpeed", "Max5Speed", new)
我在浏览数据集的几个非数字空间中看到的一些不需要的占位符示例是 "M"、“”、“-”、"null",还有一些其他随机字符串。这些都会对 convert_objects 造成问题吗? seaborn 知道忽略 NaN 吗?我不知道出了什么问题。感谢您的帮助。
您需要将结果赋值给自己:
new = new.convert_objects(convert_numeric=True)
见docs
convert_objects
现已从版本 0.21.0
开始弃用,您必须使用 to_numeric
:
new = new.convert_objects()
如果您有多列:
new = new.apply(pd.to_numeric)
我正在尝试使用天气数据、pandas 和 seaborn 创建一些图表。不过,我在使用 lmplot(或与此相关的任何其他 seaborn 绘图函数)时遇到了问题。有人告诉我它不能连接 str 和 float 对象,但我事先使用了 convert_objects(convert_numeric=True) ,所以我不确定问题是什么,当我只是打印数据框本身我没有看到任何错误。
import numpy as np
import pandas as pd
import seaborn as sns
new.convert_objects(convert_numeric=True)
sns.lmplot("AvgSpeed", "Max5Speed", new)
我在浏览数据集的几个非数字空间中看到的一些不需要的占位符示例是 "M"、“”、“-”、"null",还有一些其他随机字符串。这些都会对 convert_objects 造成问题吗? seaborn 知道忽略 NaN 吗?我不知道出了什么问题。感谢您的帮助。
您需要将结果赋值给自己:
new = new.convert_objects(convert_numeric=True)
见docs
convert_objects
现已从版本 0.21.0
开始弃用,您必须使用 to_numeric
:
new = new.convert_objects()
如果您有多列:
new = new.apply(pd.to_numeric)