python
Get column names from Pandas DataFrame as a python List
To get column names from a Pandas DataFrame you can use df.columns.values and if you want to convert it to a list you can use df.columns.values.tolist()
import pandas as pd
df = pd.DataFrame({
'Name': ['John', 'Eric', 'Rick'],
'Score': [200, 500, 100\]
})
df_columns = df.columns.values.tolist()
print(df_columns)
#prints - ['Name', 'Score']
Output
['Name', 'Score']
In the code snippet, we have created a DataFrame df which has two columns Name and Score. If we want to get the list of column names we can use the below syntax.
df.columns.values.tolist()
Was this helpful?
Similar Posts
- Change column orders using column names list - Pandas Dataframe
- Reorder dataframe columns using column names in pandas
- Add suffix/prefix to column names of Pandas DataFrame
- [Pandas] Add new column to DataFrame based on existing column
- Get a column rows as a List in Pandas Dataframe
- Get column values as list in Pandas DataFrame
- Pandas - Delete,Remove,Drop, column from pandas DataFrame