只打印对象的一部分

2024-09-28 05:27:07 发布

您现在位置:Python中文网/ 问答频道 /正文

使用python3.8.0,此代码

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

给出结果:

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

这个代码是:

print(p.price)

提供:

"AUPH    19.8893
 Name: price, dtype: float64"

如何只访问p中的浮点数(19.8893),以便只打印数字?你知道吗


Tags: 代码getlanguagemarketpriceregionyahooen
3条回答

假设您有以下字符串:

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

如果您使用split方法,您将拥有:

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

因此,对于您的情况,您可以通过执行以下操作获得号码:

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

试试这个:

print(p.price.values[0])

因为p.priceSeries object,所以可以使用所有可用的方法。你知道吗

这是获取值的正确方法

import pandas_datareader as pdr

p = pdr.get_quote_yahoo('AUPH')

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

或者

import pandas_datareader as pdr

p = pdr.get_quote_yahoo('AUPH')

print(p.price.get(0))

或者

import pandas_datareader as pdr

p = pdr.get_quote_yahoo('AUPH')

print(p.price[0])

输出

# > python test.py
20.01

相关问题 更多 >

    热门问题