How to print a column in python pandas

I created the following Series and DataFrame:

import pandas as pd

Series_1 = pd.Series({'Name': 'Adam','Item': 'Sweet','Cost': 1})
Series_2 = pd.Series({'Name': 'Bob','Item': 'Candy','Cost': 2})
Series_3 = pd.Series({'Name': 'Cathy','Item': 'Chocolate','Cost': 3})`
df = pd.DataFrame([Series_1,Series_2,Series_3], index=['Store 1', 'Store 2', 'Store 3'])

I want to display/print out just one column from the DataFrame (with or without the header row):

Either

Adam 
Bob 
Cathy

Or:

Sweet
Candy
Chocolate

I have tried the following code which did not work:

print(df['Item'])
print(df.loc['Store 1'])
print(df.loc['Store 1','Item'])
print(df.loc['Store 1','Name'])
print(df.loc[:,'Item'])
print(df.iloc[0])

Can I do it in one simple line of code?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    In this article, we will discuss how to select a single column in a data frame. Now let us try to implement this using Python.

    First, let’s create a dataframe 

    Python3

    import pandas as pd

    df = pd.DataFrame({'name': ['Akash', 'Ayush', 'Ashish',

                                'Diksha', 'Shivani'],

                       'Age': [21, 25, 23, 22, 18],

                       'Interest': ['Coding', 'Playing', 'Drawing',

                                    'Akku', 'Swimming']})

    print("The original data frame")

    df

    Output:

    How to print a column in python pandas

    Method 1: Using Dot(dataframe.columnname) returns the complete selected column 

    Python3

    print("Single column value using dataframe.dot")

    print(df.Interest)

    Output:

    How to print a column in python pandas

    Method 2: Using dataframe[columnname] method: 
    There are some problems that may occur with using dataframe.dot are as follows: 

    • Through dot method, we cannot Select column names with spaces. 
    • Ambiguity may occur when we Select column names that have the same name as methods for example max method of dataframe. 
    • We cannot Select multiple columns using dot method. 
    • We cannot Set new columns using dot method.

    Because of the above reason dataframe[columnname] method is used widely.

    Python3

    print("Single column value using dataframe[]")

    print(df['Interest'])

    Output:

    How to print a column in python pandas

    Another Example now if we want to select the column Age.

    Python3

    print("Single column value using dataframe[]")

    print(df['Age'])

    Output:

    How to print a column in python pandas


    How do I print a column in pandas Python?

    columns to print column names in Python. We can use pandas. dataframe. columns variable to print the column tags or headers at ease.

    How do you print an entire column in Python?

    Use pandas. max_columns", max_cols) with both max_rows and max_cols as None to set the maximum number of rows and columns to display to unlimited, allowing the full DataFrame to be displayed when printed.

    How do I print a specific column?

    On the Ribbon, click the Page Layout tab. In the Sheet Options group, under Headings, select the Print check box. , and then under Print, select the Row and column headings check box .

    How do I display a specific column in Python?

    If you have a DataFrame and would like to access or select a specific few rows/columns from that DataFrame, you can use square brackets or other advanced methods such as loc and iloc .