- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在服务器上使用 jupyter notebook(文件夹不在我的电脑上)。我有一个包含 30 个数据框的文件夹,这些数据框具有完全相同的列。它们都保存在下一个路径中:
Reut/folder_no_one/here_the_files_located
我想将它们全部打开并连接起来。我知道我可以做这样的事情:
df1=pd.read_pickle('table1')
df2=pd.read_pickle('table2')
df3=pd.read_pickle('table3')
...
#and then concat
但我确信有更好、更聪明的方法来做到这一点。我试图打开所有文件并将它们分别保存如下:
num=list(range(1, 33)) #number of tables I have in the folder
path_to_files=r'Reut/here_the_files_located'
Path=r'Reut/folder_no_one/here_the_files_located'
{f"df{num}" : pd.read_pickle(file) for num, file in enumerate(Path(path_to_files).glob('*.pickle'))}
但是我得到了这个错误:
--------------------------------------------------------------------------- TypeError Traceback (most recent calllast) in ----> 1 {f"df{num}" : pd.read_pickle(file) for num, file in enumerate(Path(path_to_files).glob('*.pickle'))}
TypeError: 'str' object is not callable
我试过玩和放不同版本的路径,也没有放路径(因为我的笔记本是那些文件所在的地方),但我总是遇到同样的错误。
*值得一提的是,当笔记本也在该文件夹中时,我可以在不指定路径的情况下打开这些文件。
我的最终目标是自动将所有这些表打开并合并为一个大表。
编辑:我也试过这个:
path = r'file_name/file_location_with_all_pickles'
all_files = glob.glob(path + "/*.pkl")
li = []
for filename in all_files:
df = pd.read_pickle(filename)
li.append(df)
frame = pd.concat(li, axis=0, ignore_index=True)
还有
path_to_files = r'file_name/file_location_with_all_pickles'
tables = []
for table in pathlib.Path(path_to_files).glob("*.pkl"):
print(table)
tables.append(pd.read_pickle(table))
但是这两种情况我都报错
ValueError: No objects to concatenatewhen I try to concat. also when I tell it to print the filename/table it does nothing. also if inside the loop I try to print just ordinary string (like print('hello'), nothing happens.it seems like there is problem with the path but when I open one specific pickle like this:
pd.read_pickle(r'file_name/file_location_with_all_pickles/specific_table.pkl')
它打开了。
'更新:
这最终对我有用:
import pandas as pd
import glob
path = r'folder' # use your path
all_files = glob.glob(path + "/*.pkl")
li = []
for filename in all_files:
df = pd.read_pickle(filename)
li.append(df)
frame = pd.concat(li, axis=0, ignore_index=True)
从这里 ( Open multiple pickle files from Jupyter notebook folder doesn't work )
最佳答案
怎么样:
path_to_files = r'Reut/here_the_files_located'
df = pd.concat([pd.read_pickle(f'{path_to_files}/table{num}.pickle') for num in range(1, 33)])
这相当于:
path_to_files = r'Reut/here_the_files_located'
tables = []
for num in range(1, 33):
filename = f'{path_to_files}/table{num}.pickle'
print(filename)
tables.append(pd.read_pickle(filename))
df = pd.concat(tables)
输出:
Reut/here_the_files_located/table1.pickle
Reut/here_the_files_located/table2.pickle
Reut/here_the_files_located/table3.pickle
Reut/here_the_files_located/table4.pickle
Reut/here_the_files_located/table5.pickle
Reut/here_the_files_located/table6.pickle
Reut/here_the_files_located/table7.pickle
Reut/here_the_files_located/table8.pickle
Reut/here_the_files_located/table9.pickle
Reut/here_the_files_located/table10.pickle
Reut/here_the_files_located/table11.pickle
Reut/here_the_files_located/table12.pickle
Reut/here_the_files_located/table13.pickle
Reut/here_the_files_located/table14.pickle
Reut/here_the_files_located/table15.pickle
Reut/here_the_files_located/table16.pickle
Reut/here_the_files_located/table17.pickle
Reut/here_the_files_located/table18.pickle
Reut/here_the_files_located/table19.pickle
Reut/here_the_files_located/table20.pickle
Reut/here_the_files_located/table21.pickle
Reut/here_the_files_located/table22.pickle
Reut/here_the_files_located/table23.pickle
Reut/here_the_files_located/table24.pickle
Reut/here_the_files_located/table25.pickle
Reut/here_the_files_located/table26.pickle
Reut/here_the_files_located/table27.pickle
Reut/here_the_files_located/table28.pickle
Reut/here_the_files_located/table29.pickle
Reut/here_the_files_located/table30.pickle
Reut/here_the_files_located/table31.pickle
Reut/here_the_files_located/table32.pickle
关于您的代码的一些评论:
num=list(range(1, 33)) #number of tables I have in the folder
path_to_files=r'Reut/here_the_files_located'
Path=r'Reut/folder_no_one/here_the_files_located'
{f"df{num}" : pd.read_pickle(file) for num, file in enumerate(Path(path_to_files).glob('*.pickle'))}
num=list(range(1, 33)) #number of tables I have in the folder
不需要用range
创建一个list
。直接在 for 循环或列表/字典理解中使用 range
效果很好。
Path=r'Reut/folder_no_one/here_the_files_located'
我猜您之前已经从 pathlib
中导入了 Path
类。如果您想像平常一样调用 Path
,则需要为该变量选择另一个名称。这就是您收到错误 TypeError: 'str' object is not callable
的原因。
is there nay way to use it if the tables names' are not the same? e.g if one was table1 and one is dataframe3, just to read them not depended on their name
当然。假设所有已保存表格的文件名都以 .pickle
结尾,您可以使用 glob
像你第一次尝试的方法。不要忘记import pathlib
。
import pathlib
path_to_files = r'Reut/here_the_files_located'
tables = []
for table in pathlib.Path(path_to_files).glob("*.pickle"):
tables.append(pd.read_pickle(table))
df = pd.concat(tables)
关于python - 从 Jupyter notebook 文件夹打开多个 pickle 文件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64192388/
我在使用NetBeans 6.8时遇到以下问题。我通过项目属性->库->编译选项卡->添加JAR /文件夹添加带有jar的文件夹。在下一个窗口中,我选择文件夹,然后选择“复制到库文件夹”。但是,我仍然
我的网站有一个域别名。我想知道如何将 domainA.ext 的请求重定向到 https://domainA.ext/folderA和对 domainB.ext 的请求到 http://domainB
我应该在 Eclipse 中构建的 Android 项目中创建自己的自定义菜单文件夹吗?例如,我想创建一种出现在所有 Activity 中的标题。我知道菜单应该在 res/menu 文件夹中的 XML
我正在使用 VS2008 和 .net 3.5。我在我的解决方案中创建了一个类库(Myproject.Controllers)。在这个类下,我添加了一个 Controllers 文件夹。在文件夹中我添
我有一个包含生成后步骤的 Visual Studio 2012 扩展项目,我想在其中将 .dll 和 .AddIn 文件复制到当前用户的 Visual Studio 2012 AddIns 文件夹中。
我在专有的 linux 发行版中有一些自动下载。 他们去临时暂存盘。我想在它们完成后将它们 move 到主 RAID 阵列。我能看到的最好方法是检查磁盘上的文件夹,看看内容是否在最后一分钟发生了变化。
我目前正在使用 SVN 对我的软件项目进行版本控制。在一个正在进行的项目中,我有主干,用于客户的共同功能和规范以及分支,用于客户特定的。 有没有办法在每次执行此类操作时标记一些不应合并到分支中的文
这个问题在这里已经有了答案: How to exclude a directory in find . command (45 个回答) 8 年前关闭。 如何删除文件夹中的所有内容并排除特定文件夹和文
如何在特定目录中创建具有当前日期和时间的文件夹或文件? DateTimeFormatter f = DateTimeFormatter.ofPattern("uuuuMMdd HHmmss") ; L
有没有办法在系统文件资源管理器的左侧“文件夹”栏中打开文件或文件夹?如果没有这个,我必须打开文件资源管理器并一直导航到该文件夹所在的位置才能操作文件,这确实很不方便。对于大多数带有这样导航栏的工具
预期:我使用 go get 安装包,它在 src 文件夹中创建了所有必要的文件夹,但它们只出现在 pkg/mod 文件夹中,我不能使用它们。 现实:它说它正在下载,完成,然后什么都没有。 一切都在 W
说 foo.zip包含: a b c |- c1.exe |- c2.dll |- c3.dll 哪里a, b, c是文件夹。 如果我 Expand-Archive .\foo.zip -Destin
不久前我正在删除 var 文件夹中 Magento 的缓存。我可能是错的,但我认为我犯了一个错误,而不是删除 var/cache 中的所有内容,而是意外删除了 var 中的所有内容。 Magento
我在 svn 存储库的单独文件夹中有一些代码项目。 现在我在删除文件时遇到一些问题:大多数时候一切顺利,但有时当我从磁盘删除文件或文件夹时, checkin 过程会出现各种错误。 所以我想知道:在sv
有没有什么方法可以用很少的R命令行自动删除所有文件或文件夹?我知道 unlink() 或 file.remove() 函数,但对于这些函数,您需要定义一个字符向量,其中包含您想要的文件的所有名称删除。
用于在文件夹中查找不符合Get-Childitem的LastWriteTime过滤器日期范围标准的文件的powershell命令是什么? 因此,请检查目录中是否包含不包含在01/10/2012(十月1
我正在为我工作的公司内部使用的应用程序之一编写 NSIS 安装程序,安装过程工作正常,所有 REG 键都已创建,文件夹和服务也没有问题,该应用程序使用。出于某种我无法理解的原因,卸载过程不起作用。
我有一个 Excel 文件,并且在同一文件夹中还有一个包含我想要包含的 CSV 文件的文件夹。使用“来自文件夹”查询,第一步将给出以下查询: = Folder.Files("D:\OneDrive\D
我在docker中玩ScyllaDB。为了使ScyllaDB在docker生产设置中最有效地运行,它需要一个XFS格式的磁盘。 您知道如何在Linux和MacO中创建XFS容器卷,磁盘文件吗? 谢谢
我应该编写一个函数,其中包含之前每次与该数字相乘的乘积 基本上是这样的: > productFromLeftToRight [2,3,4,5] [120,60,20,5] 我应该使用高阶函数,例如折叠
我是一名优秀的程序员,十分优秀!