我正在尝试保存和加载pandas包含列的 MultiIndex(2 级索引)的 DataFrame。我在保存和加载数据帧时遇到问题(如果可能的话,我希望在重新加载数据帧时拥有完全相同的数据帧)
我的数据框如下所示:
> df.head()
A B
sp start end sp start end
0 V5894_1 243 251 V5894_1 243 251
1 V5894_1 244 252 V5894_1 244 252
2 V5894_1 244 252 V5894_1 244 252
3 V3246_0 28 36 V3246_0 28 36
4 V3246_0 29 37 V3246_0 29 37
Run Code Online (Sandbox Code Playgroud)
我现在尝试的是常规的df.to_csv("test.csv"),然后用 加载它df.read_csv("test.csv",index_col=[0,1])。
当我保存它时,.csv 文件如下所示:
,A,A,A,B,B,B
,sp,start,end,sp,start,end
0,V5894_1,243,251,V5894_1,243,251
1,V5894_1,244,252,V5894_1,244,252
2,V5894_1,244,252,V5894_1,244,252
3,V3246_0,28,36,V3246_0,28,36
Run Code Online (Sandbox Code Playgroud)
所以我已经觉得这个结构可能已经有点破损了。
当我使用前面的命令加载它时,我得到:
A.1 A.2 B B.1 B.2
A
NaN sp start end sp start …Run Code Online (Sandbox Code Playgroud) 我刚开始学习 python 并且有一些我似乎无法弄清楚的递归问题。最烦人的是:我需要构建一个函数ind(e,L),其中e是一个 int 并且L是一个列表。
通过输入e它是否在列表中,输出需要是它的索引 例如:
ind(42,[0,14,52,42,15]) -> 3
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止编写的代码,但我得到的索引始终为 0。有人可以向我解释我做错了什么吗?
def location(e,L):
if L == []:
return False
elif e == L[0]:
A = L[:-1].index(e)
return A
else:
return location(e,L[1:])
print(location(14,[1,2,14,1]))
Run Code Online (Sandbox Code Playgroud)
谢谢 :)
我目前在 unix 环境中使用 Python 2.7。我需要在 python 脚本中运行 R 脚本,但我无法使其工作,因为我的 R 模块需要先加载(使用“模块加载”)
这是我的 python 脚本:
import os
import subprocess as sp
os.system('module load R/3.2.3')
out = sp.check_output(['Rscript','test.R'], universal_newlines=True)
Run Code Online (Sandbox Code Playgroud)
我一直遇到同样的错误:“[Errno 2]没有这样的文件或目录”
感谢您的帮助 !