0

What is the best way to view the first few columns of several data frames using a for loop?

Here is what I tried:

DF_List = (df1, df2, df3)
for DF in DF_List:
    print DF.head()

Error:

File "<ipython-input-18-bed47d1fdce7>", line 6
print DF.head()
       ^
SyntaxError: invalid syntax

I also tried: DF_List = (df1, df2, df3) for DF in DF_List: DF.head()

Error:

AttributeError: 'function' object has no attribute 'head'

Note: This question is not a duplicate of the question involving the print function, because it is not dependent on the "print"- see second attempt within the code.

arkadiy
  • 746
  • 1
  • 10
  • 26
  • What version of python? – ALollz Feb 17 '19 at 21:46
  • Try print(df.head()) instead. Currently you have capital letters according to error message. Also, in Python 3 it is print() – user3212593 Feb 17 '19 at 21:48
  • @ALollz python 3 – arkadiy Feb 17 '19 at 21:51
  • @user3212593 The capital letter are consistent with my code- I just changed them for the example- edited accordingly. I tried your suggestion, but still getting an error, as follows: for df in df_List: print(df.head()) AttributeError: 'function' object has no attribute 'head' – arkadiy Feb 17 '19 at 21:52
  • 1
    @arkadiy Something is wrong with the object you are passing to the loop then. Because as the error says, it's a function. You should check what objects you have in your tuple. – user3212593 Feb 17 '19 at 21:58
  • @user3212593 thank you! you are right. – arkadiy Feb 17 '19 at 22:03

0 Answers0