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))
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
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.
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