我正在尝试使用天气数据、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)
关于python - 将 pandas 数据框转换为数字; Seaborn 无法绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31296285/