How to print one column of Pandas dataframe

In this post, we will learn How to print one column of Pandas dataframe or how to select one column of Pandas DataFrame.The Pandas is a data analytical library that store data in tabular form, and the table in Pandas is called a dataframe that contains rows and column. It is great to help explore clean and process data. It supports the integration of different file formats “csv, excel, SQL, JSON, parquet,”.To use a Pandas library first we have to install it on the local system by using the pip command “pip install pandas” and import it into our code by using “import pandas as pd” to use its functions.

1. How to print one column of Pandas dataframe


Now as we have learned about Pandas above. in the below code, We have a dataframe that represents data in rows and columns. While doing data analysis, and selecting a single column we can use the dataframe object(dfobj.Name) and to_string(index=False) to ignore the default indices of the dataframe.

import pandas as pd
 
data = {
    'Name': ['Jack', 'Rack', 'Max', 'David'],
    'Marks':[97,97,100,100],
    'Subject': ['Math', 'Math', 'Math', 'Phy']
}
 
dfobj = pd.DataFrame(data)

print(dfobj.Name.to_string(index=False))

Output

Jack
  Rack
   Max
 David

2. How to print a column of Pandas dataframe


To print a column of Pandas dataframe,we have to pass the name of columns in between index notation (dfobj[‘Name’].to_string(index=False) and the to_string(index=False) to ignore default index of dataframe.

import pandas as pd
 
data = {
    'Name': ['Jack', 'Rack', 'Max', 'David'],
    'Marks':[97,97,100,100],
    'Subject': ['Math', 'Math', 'Math', 'Phy']
}
 
dfobj = pd.DataFrame(data)

print(dfobj['Name'].to_string(index=False)

Output

Jack
  Rack
   Max
 David

3. How to print a column of Pandas dataframe


In this example we will understand how to get value of a column of Pandas data frame, we have to pass the name of columns in between index notation dfobj[‘Name’] and to_csv(index=False) to ignore the default row index.

import pandas as pd
 
data = {
    'Name': ['Jack', 'Rack', 'Max', 'David'],
    'Marks':[97,97,100,100],
    'Subject': ['Math', 'Math', 'Math', 'Phy']
}
 
dfobj = pd.DataFrame(data)

print(dfobj['Name'].to_csv(index=False))


Output

Name
Jack
Rack
Max
David

4. How to print one column of Pandas dataframe using for Loop


In this python example to print values of one column of Pandas dataframe .The for loop is used that will iterate over the value of column ‘Name’ and print statement will display the output.

import pandas as pd
 
data = {
    'Name': ['Jack', 'Rack', 'Max', 'David'],
    'Marks':[97,97,100,100],
    'Subject': ['Math', 'Math', 'Math', 'Phy']
}
 
dfobj = pd.DataFrame(data)


for colval in dfobj['Name']:
     print(colval)

Output

Jack
  Rack
   Max
 David

Summary

In this post we have learned How to print one column of Pandas dataframe by using 4 different ways. The Pandas is a machine learning and data analytics library of Python. The table in Pandas library knows as dataframe store data in tabular form that contains rows and columns.