小编Iul*_*ana的帖子

递归导入:'import'与'from ... import ...'

我有两个文件需要使用彼此不同的功能.

file1.py:

import file2   # from file2 import y2

def x1():
    print "x1"

def x2():
    print "x2"
    file2.y2()
Run Code Online (Sandbox Code Playgroud)

file2.py:

import file1   # from file1 import x1

def y1():
    file1.x1()
    print "y"

def y2():
    print "y2"

if __name__ == "__main__":
    y1()
Run Code Online (Sandbox Code Playgroud)

我想知道为什么使用import file1工作,但从file1(from file1 import x1)导入只是特定的功能不是?

Traceback (most recent call last):
  File "file2.py", line 1, in <module>
    from file1 import x1
  File "file1.py", line 1, in <module>
    import file2
  File "file2.py", line 1, in <module> …
Run Code Online (Sandbox Code Playgroud)

python python-import

5
推荐指数
1
解决办法
2466
查看次数

Pandas,将多索引之一移动到多列索引之上

如果我有数据框的多级列和多级索引

column_level1               a1      | a2
                           ----+----|----+----
column_level2               b1 | b2 | b3 | b4

index1 | index2 | index3
-------+--------+--------+-----+----+----+-----
   0   |   c1   |   d1   |  1  |  2 |  3 |  4  |
   0   |   c2   |   d3   |  5  |  6 |  7 |  8  |    
Run Code Online (Sandbox Code Playgroud)

如何重塑数据框以将索引之一移动到 columns_level 之上?假设我希望将当前的索引2 放置在column_level0 上。

我还需要一些有效的解决方案来解决这个问题。

我当前的解决方案是按以下方式使用 stack/unstack:

df.stack().stack().unstack(index2).unstack().unstack()
Run Code Online (Sandbox Code Playgroud)

但是在巨大的数据帧上使用这种实现方式最终会消耗大量的 RAM 并花费大量的时间。

python pandas

4
推荐指数
1
解决办法
3805
查看次数

标签 统计

python ×2

pandas ×1

python-import ×1