0 1 2 5 1 2 3 9 2 8 7 2 3 3 4 7 4 6 7 7 5 4 8 3 6 8 2 8 7 9 9 10 8 6 6 4 9 10 10 7
my_dataframe y gdp cap 'y', 'gdp', 'cap']
header_list [n = []
for i in my_dataframe.columns:
n.append(i)
print n
For a quick, neat, visual check, try this:
for col in df.columns:
print col
Using list(df) print(list(df)) Output: ['Name', 'Symbol', 'Shares']
Using df. columns. values. tolist() print(df. columns. values. ...
Using list comprehension. You can also get the columns as a list using list comprehension. print([col for col in df]) Output: ['Name', 'Symbol', 'Shares']
df.columns.tolist()
>>> list(my_dataframe)
['y', 'gdp', 'cap']
To list the columns of a dataframe while in debugger mode, use a list comprehension:
>>> [c for c in my_dataframe]
['y', 'gdp', 'cap']
By the way, you can get a sorted list simply by using sorted
:
>>> sorted(my_dataframe)
['cap', 'gdp', 'y']