python - 在Python中迭代字典值

标签 python python-3.x pandas finance stock

我有 1000 多个文本文件,每个文件都有两列,一列是日期,另一列是股票价格(在各自的日期)。我已上传它们并使用以下代码将它们转换为字典:

filelist = os.listdir(r'insertfilepath')
filepath = r'insertfilepath'

dic1 = {}

for file in filelist:
    df = pd.read_csv(filepath + file,sep='\t')
    dic1[file]= df

但是,当我尝试使用以下代码更改每个列的列名称时:

for value in dic1:
    for df in value: 
        df.rename(columns={df.columns[0]:'Dates',df.columns[1]:'Prices'},inplace=True)

我收到以下错误:“str”对象没有属性“rename”。

有趣的是,当我将其转换为列表时,这有效,但是我无法分析列表中的数据,因为我无法知道哪只股票与哪些数据匹配。

任何帮助将不胜感激。我是 python 新手,所以请放轻松。谢谢

最佳答案

for value in dic1循环遍历键,在本例中是文件名字符串。您想要for value in dic1.values()

如果您想要键和值,请使用 dic1.items()它返回(键,值)元组

关于python - 在Python中迭代字典值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60240585/

相关文章:

python - 没有名为 _backend_gdk 的模块

python - 获取AWS S3中所有存储桶的名称和数量

python - python排序方法中的键如何使用列表

python - Storm Python 3 兼容吗?

python - 如何使用函数的参数来确定调用哪个类?

python - 在 Pandas 中,如何制作数据透视表来计数和跳过重复?

Python 模块错误仅在 Windows 10 中出现?

python - Tarjan 算法 - Python 到 scala

python - 在不删除行的情况下过滤 Pandas DataFrame

python - 通过替换计算两个数据框中两列的减法