首页 > 解决方案 > 字符串没有属性 .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”

标签: pythonpandasmatplotlibohlc

解决方案


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[:, :]

推荐阅读