The Wayback Machine - https://web.archive.org/web/20240710002502/https://www.geeksforgeeks.org/how-to-select-single-column-of-a-pandas-dataframe/
Open In App

How to Select single column of a Pandas Dataframe?

Last Updated : 01 Oct, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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




# importing pandas as library
import pandas as pd
  
  
# creating data frame:
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:

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

Python3




# using dot method
print("Single column value using dataframe.dot")
print(df.Interest)


Output:

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




# using dataframe[columnname]method
print("Single column value using dataframe[]")
print(df['Interest'])


Output:

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

Python3




# using dataframe[columnname]method
print("Single column value using dataframe[]")
print(df['Age'])


Output:



Previous Article
Next Article

Similar Reads

Select a single column of data as a Series in Pandas
In this article, we will discuss how to select a single column of data as a Series in Pandas. For example, Suppose we have a data frame : Name Age MotherTongue Akash 21 Hindi Ashish 23 Marathi Diksha 21 Bhojpuri Radhika 20 Nepali Ayush 21 Punjabi Now when we select column Mother Tongue as a Series we get the following output: Hindi Marathi Bhojpuri
1 min read
Select all columns, except one given column in a Pandas DataFrame
DataFrame Data structure are the heart of Pandas library. DataFrames are basically two dimension Series object. They have rows and columns with rows representing the index and columns representing the content. Now, let's see how to Select all columns, except one given column in Pandas DataFrame in Python. Creating a DataFrame: C/C++ Code # import p
2 min read
Get column index from column name of a given Pandas DataFrame
In this article we will see how to get column index from column name of a Dataframe. We will use Dataframe.columns attribute and Index.get_loc method of pandas module together. Syntax: DataFrame.columns Return: column names index Syntax: Index.get_loc(key, method=None, tolerance=None) Return: loc : int if unique index, slice if monotonic index, els
2 min read
Create a Pandas DataFrame from a Numpy array and specify the index column and column headers
This article demonstrates multiple examples to convert the Numpy arrays into Pandas Dataframe and to specify the index column and column headers for the data frame. Example 1: In this example, the Pandas dataframe will be generated and proper names of index column and column headers are mentioned in the function. This approach can be used when ther
4 min read
Convert given Pandas series into a dataframe with its index as another column on the dataframe
First of all, let we understand that what are pandas series. Pandas Series are the type of array data structure. It is one dimensional data structure. It is capable of holding data of any type such as string, integer, float etc. A Series can be created using Series constructor. Syntax: pandas.Series(data, index, dtype, copy) Return: Series object.
1 min read
How to select rows from a dataframe based on column values ?
Prerequisite: Pandas.Dataframes in Python In this article, we will cover how we select rows from a DataFrame based on column values in Python. The rows of a Dataframe can be selected based on conditions as we do use the SQL queries. The various methods to achieve this is explained in this article with examples. Importing Dataset for demonstration T
5 min read
Select specific column of PySpark dataframe with its position
In this article, we will discuss how to select a specific column by using its position from a pyspark dataframe in Python. For this, we will use dataframe.columns() method inside dataframe.select() method. Syntax: dataframe.select(dataframe.columns[column_number]).show() where, dataframe is the dataframe namedataframe.columns[]: is the method which
2 min read
How to select the rows of a dataframe using the indices of another dataframe?
Prerequisites: PandasRandom Using Pandas module it is possible to select rows from a data frame using indices from another data frame. This article discusses that in detail. It is advised to implement all the codes in jupyter notebook for easy implementation. Approach:Import moduleCreate first data frame. In the example given below choice(), randin
2 min read
Split single column into multiple columns in PySpark DataFrame
pyspark.sql.functions provide a function split() which is used to split DataFrame string Column into multiple columns. Syntax: pyspark.sql.functions.split(str, pattern, limit=- 1) Parameters: str: str is a Column or str to split.pattern: It is a str parameter, a string that represents a regular expression. This should be a Java regular expression.l
4 min read
Select row with maximum and minimum value in Pandas dataframe
Let's see how can we select rows with maximum and minimum values in Pandas Dataframe with help of different examples using Python. Creating a Dataframe to select rows with max and min values in Dataframe C/C++ Code # importing pandas and numpy import pandas as pd import numpy as np # data of 2018 drivers world championship dict1 = {'Driver': ['Hami
2 min read
three90RightbarBannerImg