简体   繁体   中英

Printing only one part of an object

Using Python 3.8.0, this code

p = get_quote_yahoo("AUPH")
print(p) 

gives result:

"language region quoteType  ...     market esgPopulated   price
AUPH    en-US     US    EQUITY  ...  us_market        False  19.825
[1 rows x 60 columns]"

and this code:

print(p.price)

gives:

"AUPH    19.8893
 Name: price, dtype: float64"

How do I access only the floating number ( 19.8893 ) in p so that only the number prints?

尝试这个:

print(p.price.values[0])

Since p.price is a Series object you can use all the methods available.

This is the correct way to get the value

import pandas_datareader as pdr

p = pdr.get_quote_yahoo('AUPH')

print(next(iter(p.price)))

Or

import pandas_datareader as pdr

p = pdr.get_quote_yahoo('AUPH')

print(p.price.get(0))

Or

import pandas_datareader as pdr

p = pdr.get_quote_yahoo('AUPH')

print(p.price[0])

Outputs

# > python test.py
20.01

Let assume you have this string:

m = """AUPH    19.8893
    Name: price, dtype: float64"""

If you use split method you will have:

>>> m.split()
['AUPH', '19.8893', 'Name:', 'price,', 'dtype:', 'float64']
>>> m.split()[1]
'19.8893'
>>> float(m.split()[1])
19.8893

So, for your case, You can get the number by doing:

m = p.price
result = float(m.split()[1])
print(result) # will display 19.8893

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