python - 字符串没有属性 .loc
问题描述
我有不同的数据框遵循这个例子:'AAPLdf,AMZNdf,GOOGLdf,......'等等
import matplotlib.pyplot as plt
from mpl_finance import candlestick_ohlc
import pandas as pd
import matplotlib.dates as mpl_dates
ticker = input("ticker:") + "df"
data = ticker
ohlc = data.loc[:, ['t', 'o', 'h', 'l', 'c']]
AttributeError:“str”对象没有属性“loc”
解决方案
eval()
是一个内置的python函数f'{variable}'
是一个 f 字符串
ticker = input('ticker: ')
# this will return data as the dataframe from ticker, but it must be an exact match
data = eval(f'{ticker}df') # -> equivalent to eval('GOOGLdf`), for example
# now you can use .loc
data.loc[:, :]
推荐阅读
- javascript - 有没有办法在javascript中将文本转换为数字,然后将数字转换为二进制?
- php - 警告:substr_count():第 33 行 C:\xampp\htdocs\tugasakhirhotel\coba\pesan\index.php 中的空子字符串
- azure-sql-database - SSMS 中的 SQLCMD 不起作用;连接到 Azure SQL 数据库
- javascript - 如何禁用鼠标滚轮滚动
- azure-devops - Azure-DevOps 无需访问代码即可查看历史记录的权限
- java - 数字金字塔,无法将数字打印到左侧
- vba - 如何从桌面目录中的所有电子邮件模板中删除特定收件人?
- c# - Parasoft 无法识别自定义 IEnumerable 扩展方法 .IsNullOrEmpty()
- vb.net - 同时运行两个事件
- browser-cache - 外部加载资源 (JS/CSS) 缓存控制 TTL 设置在哪里?