How do i print a list of column names in python?
View Discussion Show
Improve Article Save Article View Discussion Improve Article Save Article While analyzing the real datasets which are often very huge in size, we might need to get the column names in order to perform some certain operations. Let’s discuss how to get column names in Pandas dataframe. First, let’s create a simple dataframe with nba.csv file. Python3Now let’s try to get the columns name from above dataset. Method #1: Simply iterating over columns Python3
Output:
Method #2: Using columns attribute with dataframe object Python3
Output:
Method #3: Using keys() function: It will also give the columns of the dataframe. Python3Output: Method #4: column.values method returns an array of index. Python3
Output: Method #5: Using tolist() method with values with given the list of columns. Python3
Output:
Method #6: Using sorted() method : sorted() method will return the list of columns sorted in alphabetical order. Python3
Output:
How to get or print Pandas DataFrame Column Names? You can get the Pandas DataFrame Column Names by using To get a list of columns from the DataFrame header use
1. Quick Examples of Get Column NamesFollowing 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
You can get the column names from pandas DataFrame using Note that To convert an array of column names into a list, we can use either
Yields below output.
You can also use
3. Use list(df) to Get Column Names from DataFrameUse
4. Get Column Names in Sorting orderIn order to get a list of column names in a sorted order use
Yields below output. Notice the difference of output from above.
5. Access All Column Names by IteratingSometimes you may need to iterate over all columns and apply some function, you can do this as below.
Yields below output.
6. Get Column Headers Using the keys() Method
Yields below output.
7. Get All Numeric Column NamesSometimes 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
Yields below output.
Use for
Yields same output as above. 9. Complete Example of pandas Get Columns Names
ConclusionIn this article, you have learned how to get or print the column names using Happy Learning !! You May Also Like
References
How do I get a list of column names in Python?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.
How do I print a list of column names?First, we use the DataFrame.columns method to print all names:. Get the Column Names Using the columns() Method. ... . Using the keys() Method. ... . By Iterating of the Columns. ... . Using list() to Print the Names as a list. ... . Using tolist() to Print the Names as a List. ... . Using sorted() to Get an Ordered List.. How do I print multiple columns in Python?There are three basic methods you can use to select multiple columns of a pandas DataFrame:. Method 1: Select Columns by Index df_new = df. iloc[:, [0,1,3]]. Method 2: Select Columns in Index Range df_new = df. iloc[:, 0:3]. Method 3: Select Columns by Name df_new = df[['col1', 'col2']]. How do you print columns in Python?Print With Column Alignment in Python. Use the % Formatting to Print With Column Alignment in Python.. Use the format() Function to Print With Column Alignment in Python.. Use f-strings to Print With Column Alignment in Python.. Use the expandtabs() Function to Print With Column Alignment in Python.. |