如何将数据帧从 Pandas 的一个模块导入到另一个模块?
我想重用另一个模块中数据框中的一些列。
我想要类似
file1.py
A B
25 Hello
30 How are you
file2.py
A B C
25 Hello Hola
30 How are you Como estas
我尝试这样做,但它不起作用。
file1.py
import pandas as pd
def cr():
data = {'state': ['Ohio','Ohio','Ohio','Nevada','Nevada'],
'year': [2000,2001,2002,2001,2002],
'pop': [1.5,1.7,3.6,2.4,2.9]}
df = pd.DataFrame(data)
return df
输出:
state year pop
0 Ohio 2000 1.5
1 Ohio 2001 1.7
2 Ohio 2002 3.6
3 Nevada 2001 2.4
4 Nevada 2002 2.9
file2.py
from file1 import cr
输出
ImportError: cannot import name 'cr' from 'file1'
I would like to reuse some columns from a dataframe which are in another module.
I want something like
file1.py
A B
25 Hello
30 How are you
file2.py
A B C
25 Hello Hola
30 How are you Como estas
I tried doing this, but it does not work.
file1.py
import pandas as pd
def cr():
data = {'state': ['Ohio','Ohio','Ohio','Nevada','Nevada'],
'year': [2000,2001,2002,2001,2002],
'pop': [1.5,1.7,3.6,2.4,2.9]}
df = pd.DataFrame(data)
return df
output:
state year pop
0 Ohio 2000 1.5
1 Ohio 2001 1.7
2 Ohio 2002 3.6
3 Nevada 2001 2.4
4 Nevada 2002 2.9
file2.py
from file1 import cr
output
ImportError: cannot import name 'cr' from 'file1'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
其实这看起来不错。如果您尝试以下操作
,然后在
file1.py
和file2.py
旁边的文件夹中运行python file2.py
,程序实际上应该打印foo
。但这仅在您在
file1.py
和file2.py
所在的文件夹中执行python
时才有效。Actually that looks good. If you try the following
and then run
python file2.py
in the folder next tofile1.py
andfile2.py
, the program should actually printfoo
.But this only works, if you execute
python
in the folder wherefile1.py
andfile2.py
are located.文件1.py
文件2.py
file1.py
file2.py