我有一个名为test的数组,其中包含了一个城市自1951年以来的冬季温度。我想绘制包含在名为wintermean的列中的温度,但当我尝试使用plt.plot(test.wintermean.values)时,我得到以下错误:TypeError:无法将系列转换为<class 'float'>。以下是test、wintermean和test.wintermean.values的外观:如何绘制温度数据?
test
wintermean
plt.plot(test.wintermean.values)
test.wintermean.values
zi8p0yeb1#
看起来你的DataFrame只有一行,列值是pandas.Series。要绘制该系列,请尝试:
pandas.Series
test["wintermean"].iloc[0].plot()
iyzzxitl2#
我认为你需要清理数据。可能存在一些非数值。例如“-”或“?“.首先转换那些
test['wintermean'] = pd.to_numeric(test['wintermean'], errors='coerce')
errors='coerce'会将这些非数值转换为Nan,而不是抛出错误。然后删除Nan值
errors='coerce'
Nan
# drops Nan values in `winterman` column test.dropna(subset=['wintermean'], inplace=True)
2条答案
按热度按时间zi8p0yeb1#
看起来你的DataFrame只有一行,列值是
pandas.Series
。要绘制该系列,请尝试:iyzzxitl2#
我认为你需要清理数据。可能存在一些非数值。例如“-”或“?“.首先转换那些
errors='coerce'
会将这些非数值转换为Nan
,而不是抛出错误。然后删除Nan
值