简体   繁体   中英

Plotting percentages from a csv file into matplotlib

I have a dataframe called orbital returns which I pulled from a 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

This code should work (errors will result in NaN-values - thanks for comment):

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM