13

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

7
  • pick the one that has your preferred balance of readability and performance. Commented Jan 9, 2019 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. Commented Jan 9, 2019 at 16:29
  • 1
    related: stackoverflow.com/questions/19482970/… you can do list(df) if you hate typing my_dataframe.columns.values.tolist() if you want speed Commented Jan 9, 2019 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. Commented Jan 9, 2019 at 16:34
  • 1
    @Giannis the differences and benefits are discussed at length in the other question. Commented Jan 9, 2019 at 16:34

1 Answer 1

17

You can also use:

df.columns.tolist() 
Sign up to request clarification or add additional context in comments.

2 Comments

I guess, my question is if one of them is more "correct", i.e there is some edge case that the others will fail.
None of them will fail. I just gave you the one which is quite fast.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.