7

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

4
  • 1
    Please note that python imports are case sensitive. Your import line should be from pandas import DataFrame. Also note that in your snippet you do not use this import, so you could leave it out. Commented Apr 19, 2015 at 11:24
  • 1
    Are you sure that's the code you're running? from pandas import dataframe wouldn't give you ImportError: cannot import name DataFrame... Commented Apr 19, 2015 at 11:32
  • Did you actually see an ImportError with that capitalization of "DataFrame"? Commented Apr 19, 2015 at 11:32
  • @TristanSun Btw. please accept answers if they solved your issue. Commented May 16, 2015 at 10:59

3 Answers 3

7

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)
Sign up to request clarification or add additional context in comments.

1 Comment

thanks, but this time I capitalize the command 'DataFrame'. The error still shows 'ImportError: cannot import name DataFrame'
5

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.