[英]Plotting percentages from a csv file into matplotlib
I have a dataframe called orbital returns which I pulled from a csv: 我有一个称为csv的数据帧,它是从csv中提取的:
orbitalreturns = pd.DataFrame.from_csv('Orbital returns.csv',index_col=0,header=0)
2014-02-28 NaN
2014-03-31 1.17%
2014-04-30 1.01%
2014-05-31 2.77%
2014-06-30 2.41%
2014-07-31 -5.44%
I simply want to plot it but get: 我只是想绘制它,但得到:
TypeError: Empty 'DataFrame': no numeric data to plot
I have tried: 我努力了:
orbitalreturns['OrbitalReturns'].strip('%')
but get: 但得到:
AttributeError: 'Series' object has no attribute 'strip'
To work with strings you need to use .str method as described here: https://pandas.pydata.org/pandas-docs/stable/text.html#indexing-with-str 要使用字符串,您需要按如下所述使用.str方法: https : //pandas.pydata.org/pandas-docs/stable/text.html#indexing-with-str
This code should work (errors will result in NaN-values - thanks for comment): 此代码应该可以工作(错误将导致NaN值-感谢您发表评论):
orbitalreturns['OrbitalReturns'] = pd.to_numeric(orbitalreturns['OrbitalReturns'].str.strip('%'),errors='coerce')
When printing: 打印时:
orbitalreturns["OrbitalReturns"]
You get (which looks perfectly fine): 您得到(看起来很好):
0 1.17
1 1.01
2 2.77
3 2.41
4 -5.44
Name: OrbitalReturns, dtype: float64
Inspect the values in each serie below : 检查以下每个系列中的值 :
orbitalreturns['OrbitalReturns'].values
# array([nan, '1.17%', '1.01%', '2.77%', '2.41%', '-5.44%'], dtype=object)
orbitalreturns['OrbitalReturns'].str.strip("%").values
# array([nan, '1.17', '1.01', '2.77', '2.41', '-5.44'], dtype=object)
pd.to_numeric(orbitalreturns['OrbitalReturns'].str.strip("%")).values
# array([ nan, 1.17, 1.01, 2.77, 2.41, -5.44])
Remove the % sign and convert to a floating point number: 删除%符号并转换为浮点数:
orbitalreturns['OrbitalReturns'] = orbitalreturns['OrbitalReturns']\
.str.strip('%').astype(float)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.