- Pandas Get Column Names from DataFrame
- 1. Quick Examples of Get Column Names
- 2. pandas Get Column Names
- 3. Use list(df) to Get Column Names from DataFrame
- 4. Get Column Names in Sorting order
- 5. Access All Column Names by Iterating
- 6. Get Column Headers Using the keys() Method
- 7. Get All Numeric Column Names
- 9. Complete Example of pandas Get Columns Names
- Conclusion
- Related Articles
- References
- You may also like reading:
- Как получить список всех имен столбцов в Pandas (методы 4)
- Способ 1: Используйте скобки
- Способ 2: Используйте tolist()
- Способ 3: использовать список()
- Способ 4: используйте list() со значениями столбца
- Дополнительные ресурсы
- Get Column Names as List in Pandas DataFrame
- How to get the list of column names of a Pandas dataframe?
- Examples
- Using the list() function
- Using df.columns.values.tolist()
- Using list comprehension
- Comparing the methods
- Author
Pandas Get Column Names from DataFrame
How to get or print Pandas DataFrame Column Names? You can get the Pandas DataFrame Column Names by using DataFrame.columns.values method and to get it as a list use tolist(). Each column in a Pandas DataFrame has a label/name that specifies what type of value it holds/represents. Getting a column names is useful when you wanted to access all columns by name programmatically or manipulate the values of all columns. In this article, I will explain different ways to get column names from pandas DataFrame headers with examples.
To get a list of columns from the DataFrame header use DataFrame.columns.values.tolist() method. Below is an explanation of each section of the statement.
- .columns returns an Index object with column names. This preserves the order of column names.
- .columns.values returns an array and this has a helper function .tolist() that returns a list of column names.
1. Quick Examples of Get Column Names
Following are some quick examples of how to get column names from pandas DataFrame, If you wanted to print it to console just use the print() statment.
Create a Pandas DataFrame from Dict with a few rows and with columns names Courses , Fee , Duration and Discount .
df = pd.DataFrame(technologies) print(df)
2. pandas Get Column Names
You can get the column names from pandas DataFrame using df.columns.values , and pass this to python list() function to get it as list, once you have the data you can print it using print() statement. I will take a moment to explain what is happening on this statement, df.columns attribute returns an Index object which is a basic object that stores axis labels. Index object provides a property Index.values that returns data in an array, in our case it returns column names in an array.
Note that df.columns preserve the order of the columns as-is.
To convert an array of column names into a list, we can use either .toList() on array object or use list(array object) .
You can also use df.columns.values.tolist() to get the DataFrame column names.
3. Use list(df) to Get Column Names from DataFrame
Use list(df) to get the column header from pandas DataFrame. You can also use list(df.columns) to get column names.
4. Get Column Names in Sorting order
In order to get a list of column names in a sorted order use sorted(df) function. this function returns column names in alphabetical order.
Yields below output. Notice the difference of output from above.
5. Access All Column Names by Iterating
Sometimes you may need to iterate over all columns and apply some function, you can do this as below.
6. Get Column Headers Using the keys() Method
df.keys() is another approach to get all column names as a list from pandas DataFrame.
7. Get All Numeric Column Names
Sometimes while working on the analytics, you may need to work only on numeric columns, hence you would be required to get all columns of a specific data type. For example, getting all columns of numeric data type can get using undocumented function df._get_numeric_data() .
Use for df.dtypes[df.dtypes!=»Courses»].index : This is another simple code for finding numeric columns in a pandas DataFrame.
Yields same output as above.
9. Complete Example of pandas Get Columns Names
df = pd.DataFrame(technologies) print(df) # Get the list of all column names from headers column_headers = list(df.columns.values) print("The Column Header :", column_headers) # Get the list of all column names from headers column_headers = df.columns.values.tolist() print("The Column Header :", column_headers) # Using list(df) to get the column headers as a list column_headers = list(df.columns) # Using list(df) to get the list of all Column Names column_headers = list(df) # Dataframe show all columns sorted list col_headers=sorted(df) print(col_headers) # Get all Column Header Labels as List for column_headers in df.columns: print(column_headers) column_headers = df.keys().values.tolist() print("The Column Header :", column_headers) # Get all numeric columns numeric_columns = df._get_numeric_data().columns.values.tolist() print(numeric_columns) # Simple Pandas Numeric Columns Code numeric_columns=df.dtypes[df.dtypes == "int64"].index.values.tolist() print(numeric_columns)
Conclusion
In this article, you have learned how to get or print the column names using df.columns , list(df) , df.keys , and also learned how to get all column names of type integer, finally getting column names in a sorted order e.t.c
Related Articles
References
You may also like reading:
Как получить список всех имен столбцов в Pandas (методы 4)
Вы можете использовать один из следующих четырех методов, чтобы перечислить все имена столбцов фрейма данных pandas:
Способ 1: Используйте скобки
Способ 2: Используйте tolist()
Способ 3: использовать список()
Способ 4: используйте list() со значениями столбца
В следующих примерах показано, как использовать каждый из этих методов со следующими пандами DataFrame:
import pandas as pd #create DataFrame df = pd.DataFrame() #view DataFrame df points assists rebounds blocks 0 25 5 11 6 1 12 7 8 6 2 15 7 10 3 3 14 9 6 2 4 19 12 6 7 5 23 9 5 9
Способ 1: Используйте скобки
В следующем коде показано, как перечислить все имена столбцов кадра данных pandas с помощью квадратных скобок:
[column for column in df] ['points', 'assists', 'rebounds', 'blocks']
Способ 2: Используйте tolist()
В следующем коде показано, как получить список всех имен столбцов с помощью функции .tolist() :
df.columns.values.tolist () ['points', 'assists', 'rebounds', 'blocks']
Способ 3: использовать список()
В следующем коде показано, как получить список всех имен столбцов с помощью функции list() :
list(df) ['points', 'assists', 'rebounds', 'blocks']
Способ 4: используйте list() со значениями столбца
В следующем коде показано, как перечислить все имена столбцов с помощью функции list() со значениями столбцов:
list(df.columns.values ) ['points', 'assists', 'rebounds', 'blocks']
Обратите внимание, что все четыре метода возвращают одинаковые результаты.
Обратите внимание, что для очень больших фреймов данных метод df.columns.values.tolist() работает быстрее всего.
Дополнительные ресурсы
В следующих руководствах объясняется, как выполнять другие распространенные функции со столбцами кадра данных pandas:
Get Column Names as List in Pandas DataFrame
While working with pandas dataframes it may happen that you require a list of all the column names present in a dataframe. You can use df.columns to get the column names but it returns them as an Index object. In this tutorial, we’ll show some of the different ways in which you can get the column names of a dataframe as a list which gives you more flexibility for further usage.
How to get the list of column names of a Pandas dataframe?
You can use the list() function in Python to get the column names of a Pandas dataframe as a list. Pass the dataframe as an argument to the list() function. The following is the syntax –
There are alternate methods as well to get the column names as a list. For example, you can use df.columns.values.tolist() or a list comprehension. The following is the syntax of these methods –
# Method 2 df.columns.values.tolist() # Method 3 - list comprehension [col for col in df]
Examples
Let’s now look at some examples to get the column names of a dataframe as a list.
First, let’s create a sample dataframe that we’ll be using throughout this tutorial.
import pandas as pd data = < "Name": ["Google, LLC", "Microsoft Corporation", "Tesla, Inc."], "Symbol": ["GOOG", "MSFT", "TSLA"], "Shares": [100, 50, 80], ># create dataframe df = pd.DataFrame(data) # display dataframe df
Here, df is a dataframe storing information on a sample portfolio of US companies with their Name, Stock Symbol, and the number of shares in the portfolio.
Let’s see what we get accessing the columns attribute of the dataframe df.
Index(['Name', 'Symbol', 'Shares'], dtype='object')
We see that an Index object with the column names is returned. It would be convenient if we could have it as a simple list.
Using the list() function
Pass the dataframe to the list() function to get the list of column names.
Using df.columns.values.tolist()
Alternatively, you can also use the df.columns.values.tolist() method. We know that df.columns returns an Index, now .values on it returns an array and it has a helper function .tolist() to return a list.
print(df.columns.values.tolist())
We get the same result as above.
Using list comprehension
You can also get the columns as a list using list comprehension.
Comparing the methods
Now let’s see which of the three methods shown above is the fastest. For this, we’ll be using the %timeit magic function.
%timeit list(df) %timeit df.columns.values.tolist() %timeit [col for col in df]
4.78 µs ± 592 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each) 1.03 µs ± 113 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each) 4.31 µs ± 435 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
We find that df.columns.values.tolist() is the fastest of the three. Also, note that the list() and the list comprehension methods are comparable to each other and differences might occur when working with large dataframes.
There are other ways as well to get column names as a list for a pandas dataframe but they may be more or less an extension or variation of the above three methods. For more, refer to this thread on Stack Overflow.
With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having pandas version 1.0.5
More on Pandas DataFrames –
- Pandas – Sort a DataFrame
- Change Order of Columns of a Pandas DataFrame
- Pandas DataFrame to a List in Python
- Pandas – Count of Unique Values in Each Column
- Pandas – Replace Values in a DataFrame
- Pandas – Filter DataFrame for multiple conditions
- Pandas – Random Sample of Rows
- Pandas – Random Sample of Columns
- Save Pandas DataFrame to a CSV file
- Pandas – Save DataFrame to an Excel file
- Create a Pandas DataFrame from Dictionary
- Convert Pandas DataFrame to a Dictionary
- Drop Duplicates from a Pandas DataFrame
- Concat DataFrames in Pandas
- Append Rows to a Pandas DataFrame
- Compare Two DataFrames for Equality in Pandas
- Get Column Names as List in Pandas DataFrame
- Select One or More Columns in Pandas
- Pandas – Rename Column Names
- Pandas – Drop one or more Columns from a Dataframe
- Pandas – Iterate over Rows of a Dataframe
- How to Reset Index of a Pandas DataFrame?
- Read CSV files using Pandas – With Examples
- Apply a Function to a Pandas DataFrame
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.
Author
Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects. View all posts