Python 无法导入 DataFrame
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29729185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python cannot import DataFrame
提问by Tristan Sun
I am trying to use Pandas in Python to import and manipulate some csv file.
我正在尝试在 Python 中使用 Pandas 来导入和操作一些 csv 文件。
my code is like:
我的代码是这样的:
import pandas as pd
from pandas import dataframe
data_df = pd.read_csv('highfrequency2.csv')
print(data_df.columns)
But there is an error :
但是有一个错误:
ImportError: cannot import name DataFrame
I have Pandas in Python, and I think dataframe comes with Pandas.
我有 Python 中的 Pandas,我认为数据框随 Pandas 一起提供。
So, anyone can tell me what does this error message mean ?
那么,任何人都可以告诉我这个错误信息是什么意思?
Thanks
谢谢
回答by Vaibhav Mule
You can do this
你可以这样做
import pandas as pd
data_df = pd.DataFrame(d)
or This should work from pandas import DataFrame
as module name is 'DataFrame' and it is case-sensitive.
或者这应该from pandas import DataFrame
是因为模块名称是“DataFrame”并且区分大小写。
回答by superher0
You have to use it exactly with 'DataFrame' this is really important to pay attention to the upper and lowercase characters
您必须将它与“ DataFrame”完全结合使用,这对于注意大小写字符非常重要
import pandas as pd
data_df = pd.DataFrame('highfrequency2.csv')
print(data_df.columns)
回答by cel
There are several ways to do all necessary imports for using data frames with pandas
.
有几种方法可以执行所有必要的导入,以便将数据框与pandas
.
Most people prefer this import: import pandas as pd
. This imports pandas
as an alias named pd
. Then they can use pd.DataFrame
instead of the rather verbose pandas.DataFrame
they had to write if they just used import pandas
.
大多数人更喜欢这种导入:import pandas as pd
. 这pandas
作为别名导入pd
。然后他们可以使用pd.DataFrame
而不是pandas.DataFrame
他们必须编写的相当冗长的,如果他们只是使用import pandas
.
This would be a typical code example:
这将是一个典型的代码示例:
import pandas as pd
data = {"a": [1, 2, 3], "b": [3, 2, 1]}
data_df = pd.DataFrame(data)
Of course you can pull DataFrame
into your namespace directly. You would then go with from pandas import DataFrame
. Note that python imports are case sensitive:
当然,您可以DataFrame
直接拉入您的命名空间。然后你会去from pandas import DataFrame
。请注意,python 导入区分大小写:
from pandas import DataFrame
data = {"a": [1, 2, 3], "b": [3, 2, 1]}
data_df = DataFrame(data)
Also be aware that you only have to import DataFrame
if you intend to call it directly. pd.read_csv
e.g. will always return a DataFrame
object for you. To use it you don't have to explicitly import DataFrame
first.
另请注意,仅DataFrame
当您打算直接调用它时才需要导入。pd.read_csv
eg 总是会DataFrame
为你返回一个对象。要使用它,您不必先明确导入DataFrame
。