7

I cannot find any resources about wether one of the following three methods for getting a list of column names is preferred over the others. The first and simplest, seems to work with my current example. Is there any reason I should not use it ?

>>> import pandas as pd
>>> import numpy as np
>>> df = pd.DataFrame(np.random.rand(5,3))
>>> df.columns
RangeIndex(start=0, stop=3, step=1)



>>> list(df.columns)
[0, 1, 2]
>>> df.columns.get_values().tolist()
[0, 1, 2]
>>> list(df.columns.get_values())
[0, 1, 2]

Update

Performance - related answer here: https://stackoverflow.com/a/27236748/605328

Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
Giannis
  • 5,286
  • 15
  • 58
  • 113
  • pick the one that has your preferred balance of readability and performance. – Paul H Jan 09 '19 at 16:29
  • I feel like there's no difference among the three. For me, the easier and simpler the better, so I chooose #1. – TYZ Jan 09 '19 at 16:29
  • 1
    related: https://stackoverflow.com/questions/19482970/get-list-from-pandas-dataframe-column-headers you can do `list(df)` if you hate typing `my_dataframe.columns.values.tolist()` if you want speed – EdChum Jan 09 '19 at 16:31
  • @coldspeed my question is wether all these methods are equal, or if there is a difference. I don't think its duplicate. – Giannis Jan 09 '19 at 16:34
  • 1
    @Giannis the differences and benefits are discussed at length in the other question. – user3483203 Jan 09 '19 at 16:34
  • Fair enough, should have scrolled past the first few answers. – Giannis Jan 09 '19 at 16:38
  • `[*df]` if you really hate typing – ALollz Jan 09 '19 at 16:43

1 Answers1

8

You can also use:

df.columns.tolist()
Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58