在 python 3.6.0 中将字符串插入 Dataframe

Insert string to Dataframe in python 3.6.0

我想从网络文件夹中找到所有 .gz 文件的文件夹路径。 我有以下 script 但它给出 error

TypeError: cannot concatenate a non-NDFrame object

同样请帮忙

脚本:

import os
import pandas as pd

adcPath = r'\ADC\redshift-datasets\BLSCEWAG2016'

gzPath = pd.DataFrame(columns=['Path'], dtype=object)
for path, subdirs, files in os.walk(adcPath):
for name in files:
if name.endswith('.gz'):
gzPath = gzPath.append(path) # Want to insert to dataframe gzPath to export in csv

gzPath = gzPath['Path'].unique()
exportPath = r'D:\Sunil_Work\temp8' + '\Path.csv'
gzPath.to_csv(exportPath)

您可以使用 glob 模块中的 glob 函数来获取所有 .gz 个文件:

import glob
files = glob.glob(r'\ADC\redshift-datasets\BLSCEWAG2016\**\*.gz', recursive=True)

然后,创建数据框并调用df.unique:

gzPath = pd.DataFrame(files, columns=['Path'], dtype=object)['Path'].unique()

保存为 csv:

exportPath = r'D:\Sunil_Work\temp8' + '\Path.csv'
gzPath.to_csv(exportPath)