Table of Contents
To print only one column of a Pandas DataFrame, you can use the “iloc” method and specify the column index you want to print. This will return a Series object containing the data from the selected column. Alternatively, you can use the column name as a key to access the column data. This method is useful when working with large datasets and you only need to view or analyze a specific column.
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))
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.
Cite this article
stats writer (2024). How can I print only one column of a Pandas DataFrame?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-print-only-one-column-of-a-pandas-dataframe/
stats writer. "How can I print only one column of a Pandas DataFrame?." PSYCHOLOGICAL SCALES, 27 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-print-only-one-column-of-a-pandas-dataframe/.
stats writer. "How can I print only one column of a Pandas DataFrame?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-print-only-one-column-of-a-pandas-dataframe/.
stats writer (2024) 'How can I print only one column of a Pandas DataFrame?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-print-only-one-column-of-a-pandas-dataframe/.
[1] stats writer, "How can I print only one column of a Pandas DataFrame?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I print only one column of a Pandas DataFrame?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
