python - 在python 3.6.0中将字符串插入到Dataframe中

标签 python python-3.6

我想从网络文件夹中查找所有 .gz 文件的文件夹路径。 我有以下脚本,但它给出错误:

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)    

关于python - 在python 3.6.0中将字符串插入到Dataframe中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45346863/

相关文章:

python - 如何将 bash 输出获取到变量并在 python 中隐藏输出

python - 拆分单元格值并更新现有列

python - Python 中的类继承问题

python - 为 Python3 安装 PySide

python - 类型转换 int 的奇怪行为

python3.6用unicode创建win32快捷方式

python - 按数组对象键对数组对象求和

python - BeautifulSoup 只获取 td 标签中的 "general"文本,嵌套标签中没有任何内容

python - django-cors-headers 不适用于 DRF(Django Rest Framework)

python-3.6 - Luigi 从代码中设置配置