I have a dataframe that consist of hundreds of columns, and I need to see all column names.
What I did:
In[37]:
data_all2.columns
The output is:
Out[37]:
Index(['customer_id', 'incoming', 'outgoing', 'awan', 'bank', 'family', 'food',
'government', 'internet', 'isipulsa',
...
'overdue_3months_feature78', 'overdue_3months_feature79',
'overdue_3months_feature80', 'overdue_3months_feature81',
'overdue_3months_feature82', 'overdue_3months_feature83',
'overdue_3months_feature84', 'overdue_3months_feature85',
'overdue_3months_feature86', 'loan_overdue_3months_total_y'],
dtype='object', length=102)
How do I show all columns, instead of a truncated list?
To access the names of a Pandas dataframe, we can the method columns(). For example, if our dataframe is called df we just type print(df. columns) to get all the columns of the Pandas dataframe.
You can get column names in Pandas dataframe using df. columns statement. Usecase: This is useful when you want to show all columns in a dataframe in the output console (E.g. in the jupyter notebook console).
You can globally set printing options. I think this should work:
Method 1:
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
Method 2:
pd.options.display.max_columns = None
pd.options.display.max_rows = None
This will allow you to see all column names & rows when you are doing .head()
. None of the column name will be truncated.
If you just want to see the column names you can do:
print(df.columns.tolist())
To obtain all the column names of a DataFrame, df_data
in this example, you just need to use the command df_data.columns.values
.
This will show you a list with all the Column names of your Dataframe
Code:
df_data=pd.read_csv('../input/data.csv')
print(df_data.columns.values)
Output:
['PassengerId' 'Survived' 'Pclass' 'Name' 'Sex' 'Age' 'SibSp' 'Parch' 'Ticket' 'Fare' 'Cabin' 'Embarked']
This will do the trick. Note the use of display()
instead of print.
with pd.option_context('display.max_rows', 5, 'display.max_columns', None):
display(my_df)
EDIT:
The use of display
is required because pd.option_context
settings only apply to display
and not to print
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With