How to Print One Column of a Pandas DataFrame


You can use the following methods to print one column of a pandas DataFrame:

Method 1: Print Column Without Header

print(df['my_column'].to_string(index=False))

Method 2: Print Column With Header

print(df[['my_column']].to_string(index=False)) 

Method 3: Print Column Using iloc

print(df.iloc[:, df.columns.get_loc('my_column')].to_string(index=False))

Method 4: Print Column Using Values Array

print('\n'.join(str(val) for val in df['my_column'].values))

The following examples show how to use each method in practice with the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'points': [25, 12, 15, 14, 19, 23, 25, 29, 32],
                   'assists': [5, 7, 7, 9, 12, 9, 9, 4, 5],
                   'rebounds': [11, 8, 10, 6, 6, 5, 9, 12, 8]})

#view DataFrame
print(df)

   points  assists  rebounds
0      25        5        11
1      12        7         8
2      15        7        10
3      14        9         6
4      19       12         6
5      23        9         5
6      25        9         9
7      29        4        12
8      32        5         8

Example 1: Print Column Without Header

The following code shows how to print the values in the points column without the column header:

#print the values in the points column without header
print(df['points'].to_string(index=False))

25
12
15
14
19
23
25
29
32

By using the to_string() function, we are able to print only the values in the points column without the column header or the row index values.

Example 2: Print Column With Header

The following code shows how to print the values in the points column with the column header:

#print the values in the points column with column header
print(df[['points']].to_string(index=False))

 points
     25
     12
     15
     14
     19
     23
     25
     29
     32

Notice that the values in the points column along with the column header are printed.

Note: The only difference between this example and the previous one is that we used double brackets around the column name, which allowed us to print the column header along with the values.

Example 3: Print Column Using iloc

The following code shows how to print the values in the points column using the iloc indexer:

#print the values in the points column using iloc
print(df.iloc[:, df.columns.get_loc('points')].to_string(index=False))

25
12
15
14
19
23
25
29
32

The iloc indexer is useful when you need to access a column by its position. The get_loc() method finds the integer position of the column name in the DataFrame.

This approach is handy when working with DataFrames where column positions may change dynamically during data manipulation.

Example 4: Print Column Using Values Array

The following code shows how to print the values in the points column by accessing the underlying NumPy array:

#print the values in the points column using values array
print('\n'.join(str(val) for val in df['points'].values))

25
12
15
14
19
23
25
29
32

In this approach, we access the values property of the pandas Series, which returns a NumPy array. Then we convert each value to a string and join them with newline characters.

This method is particularly useful when you need to further process the column values or integrate them with non-pandas code.

Wrapping Up

We’ve explored four different ways to print a single column from a pandas DataFrame:

  • Using the to_string() method without header (Method 1)
  • Using the to_string() method with header (Method 2)
  • Using the iloc indexer with get_loc() (Method 3)
  • Using the values array property (Method 4)

Each approach has its own advantages. Methods 1 and 2 are straightforward for everyday use. Method 3 is helpful when working with DataFrames where column positions matter. Method 4 gives you direct access to the underlying NumPy array for further processing.

Choose the method that best fits your specific needs and the context of your data analysis task.

Additional Resources

The following tutorials explain how to perform other common operations in pandas:

How to Print Pandas DataFrame with No Index
How to Show All Rows of a Pandas DataFrame
How to Check dtype for All Columns in Pandas DataFrame

Leave a Reply

Your email address will not be published. Required fields are marked *