How to print pandas column values, names and types?

Print pandas columns

You can export your one or multiple pandas DataFrame column values into a string or list object and print those in your Jupyter Notebook using the following syntax:

print(yourdf['your_col'].to_string(index=False))

# alternatively

print(yourdf['your_col'].to_list())

Step 1: Create example data

Let’s start by creating some test data that you can use to follow along:

import pandas as pd

month = ['July', 'September', 'November', 'September', 'December']
language = ['Java', 'Java', 'R', 'Javascript', 'R']
salary = [114.0, 166.0, 155.0, 158.0, 131.0]
data = dict(month = month, language = language, salary = salary)
hiring_data = pd.DataFrame(data=data)
hiring_data.head()

Step 2: Get and export column values

You can easily export the values of your month column by slicing a Series object and converting it to a string:

print(hiring_data['month'].to_string())

This will return the following string:

0         July
1    September
2     November
3    September
4     December

Step 3: Print column values not index

As you see, the row indices were exported as well. You can get rid of the index when printing by using the following syntax which prints a Numpy array containing the column values:

print(hiring_data['month'].values)

This returns the following ndarray:

['July' 'September' 'November' 'September' 'December']

Another way to export just the column values is using the index=False parameter of the to_string() function:

print(hiring_data['month'].to_string(index=False))

You can also print the values directly to a Python list using this code:

print(hiring_data['month'].to_list())

Step 4: Export DataFrame column names

You can print your DataFrame column names using the following snippet:

print(hiring_data.columns.values)

This returns an ndarray that contains the DataFRame column names:

['month' 'language' 'salary']

Step 5: Print pandas column types

In a similar fashion you can get the column data types (dypes of each of your DataFrame columns):

print((to_csv('output.csv', index=False).dtypes).to_string())

This returns the following content:

month        object
language     object
salary      float64

Step 6: Print specific columns with condition

Next we would like to print only values in a column which meet a condition. For example, all relevant values ending with the string ‘ber’. We’ll first slice the Series and then convert it to a string for easier printing.

cond = hiring_data['month'].str.endswith('ber')
print(hiring_data['month'] [cond].to_string())

Step 7: Export your Pandas column without the index

In order to export your dataframe column to a csv file you can use the to_csv Series method as shown in the snippet below.

hiring_data['month'].to_csv('output.csv', index=False)

Step 8: Extract Series values with no index

You can print the values in your Pandas Series without the index using the following snippet:

hiring_data['month'].values

FAQ

How to print columns that meet a certain condition?

To get a list of all DataFrame columns which name contains a specific substring, use the following snippet:

print(hiring_data.columns.str.contains('your_substring')

How to output the columns names of a multi-index DataFrame?

To get a list of your multi-index frame, use the following code:

print ( multi_index_df_name.columns.levels)

How to get the unique values count in each column?

You can use the nunique() function to obtain a Series containing the number of unique occurrences in a DataFrame columns; for example:

print(hiring_data['month'].nunique())