Tristan Sun
Tristan Sun

Reputation: 299

Python cannot import DataFrame

I am trying to use Pandas in Python to import and manipulate some csv file.

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.

So, anyone can tell me what does this error message mean ?

Thanks

Upvotes: 7

Views: 45954

Answers (3)

superher0
superher0

Reputation: 71

You have to use it exactly with 'DataFrame' this is really important to pay attention to the upper and lowercase characters

import pandas as pd
data_df = pd.DataFrame('highfrequency2.csv')
print(data_df.columns)

Upvotes: 7

cel
cel

Reputation: 31369

There are several ways to do all necessary imports for using data frames with 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.

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:

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.

Upvotes: 5

Vaibhav Mule
Vaibhav Mule

Reputation: 5136

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.

Upvotes: 0

Related Questions