【问题标题】:Looping through a folder in python returning blank dataframe循环遍历python中的文件夹返回空白数据框
【发布时间】:2021-12-21 15:46:07
【问题描述】:

我正在尝试获取文件夹中的所有文件,但由于某种原因,代码返回空白数据框。以下是我的代码:

def fwd_pick(p):
    
    final2 = pd.DataFrame()
    
    excel_files2 = glob.glob(p + '.xlsx')
    
    for f in excel_files2:
        df2 = pd.read_excel(f)
        final2 = final2.append(df2)
        
    return final2

# Getting the forward pick locations
fwdpick = fwd_pick('C:/Users/abc/newfolder/')

我们将不胜感激。

【问题讨论】:

  • 我正要发表评论,说明问题和答案应该如何分开。不过,我自己想通了。 *这就是回滚的原因。

标签: python pandas dataframe glob


【解决方案1】:

我意识到我错过了 glob.glob 中的 *,这就是它无法正确读取的原因。现在可以正常使用了。

def fwd_pick(p):
    
    final2 = pd.DataFrame()
    
    excel_files2 = glob.glob(p + '*.xlsx')
    
    for f in excel_files2:
        df2 = pd.read_excel(f)
        final2 = final2.append(df2)
        
    return final2

# Getting the forward pick locations
fwdpick = fwd_pick('C:/Users/abc/newfolder/')

【讨论】:

    猜你喜欢
    • 2020-11-06
    • 1970-01-01
    • 2014-04-24
    • 2017-12-28
    • 2019-09-03
    • 2023-03-23
    • 2019-04-24
    • 2019-12-12
    • 1970-01-01
    相关资源
    最近更新 更多