Get a list of a Particular Column Values of a Pandas Dataframe


Pandas is a Python Library that is used to explore and clean the messy datasets, and make the data suitable for extracting necessary and valuable insights. Dataframe in Pandas is a two-dimensional data structure which is very much similar to spreadsheets, SQL tables, and Excel Datasheets.

We can use various methods to extract the particular column values.

  • Using '.values.tolist()' method

  • Using '.loc[]' method

  • Using '.iloc[]' method

  • Using 'get()' function

Method-1: Using .values.tolist() method

The '.values' is used to extract all the associated values of a key from the python dictionary as an ordinary list or an array.

The '.tolist()' is useful in converting such an ordinary list or a numpy array into a 'Python List'.

Syntax

col_vals=df['col_name'].values.tolist()

Example

Create a table containing Student Name, Age, and Favourite Subject and extract the values of the column “Favourite Subject” using tolist() method.

Algorithm

  • First import the required libraries.

  • Create a table as per the requirement.

  • Now taking df as a variable, convert the table into a DataFrame object.

  • Apply attribute '.values' to the DataFrame df.

  • Obtained output will be a numpy array and now to turn this into a list, apply the '.tolist()' method, as our requirement is a list.

  • And lastly, use the built-in 'print()' function to print the output.

import pandas as pd
import numpy as np
#creating a table

student_data={
   'Name of the Student': ['Alice', 'Cassie', 'Henry', 'Steven'],
   'Age':[15,13,16,14],
   'Favourite Subject':['Math', 'Social', 'Science', 'English']
}

#Now, we will turn the student_data table into Dataframe.
print("DataFrame that we created:")
df=pd.DataFrame(student_data)
print(df)

#tolist() is used to convert the column values into a list.
print("Extracted values of desired Column:")
col_vals=df['Favourite Subject'].values.tolist()
print(col_vals)

Output

DataFrame that we created:
  Name of the Student  Age Favourite Subject
0               Alice   15              Math
1              Cassie   13            Social
2               Henry   16           Science
3              Steven   14           English
Extracted values of desired Column:
['Math', 'Social', 'Science', 'English']

Method-2: Using '.loc[]' method

The '.loc[]' attribute returns the specified column data of a DataFrame.

Syntax

 col_vals=df.loc[:,'col_name'].tolist()

Example

Create a table containing Student Name, Age, and Favourite Subject and extract the values of the column “Favourite Subject” using loc() method.

Algorithm

  • First import the required libraries.

  • Create a table as per the requirement.

  • Now taking df as a variable, convert the table into a DataFrame object.

  • Apply attribute 'loc' to the DataFrame df.

  • Use the '.tolist()' method to transform data into a python list, as our requirement is a list.

  • And lastly, use the built-in 'print()' function to print the output.

import pandas as pd
import numpy as np
#creating a table

student_data={
   'Name of the Student': ['Alice', 'Cassie', 'Henry', 'Steven'],
   'Age':[15,13,16,14],
   'Favourite Subject':['Math', 'Social', 'Science', 'English']
}

#Now, we will turn the student_data table into Dataframe.
print("DataFrame that we created:")
df=pd.DataFrame(student_data)
print(df)

#tolist() is used to convert the column values into a list.
print("Extracted values of desired Column:")
col_vals=df.loc[:,'Favourite Subject'].tolist()
print(col_vals)

Output

DataFrame that we created:
  Name of the Student  Age Favourite Subject
0               Alice   15              Math
1              Cassie   13            Social
2               Henry   16           Science
3              Steven   14           English
Extracted values of desired Column:
['Math', 'Social', 'Science', 'English']

Method-3: Using ‘.iloc[]’ method

The ‘.iloc[]’ attribute returns the specified column data or a row data of a DataFrame depending on the index value passed to it as a parameter.

Syntax

 col_vals=df.iloc[:,'col_index'].tolist()

Example

Create a table containing Student Name, Age, and Favourite Subject and extract the values of the column “Favourite Subject” using iloc() method.

Algorithm

  • First import the required libraries.

  • Create a table as per the requirement.

  • Now taking df as a variable, convert the table into a DataFrame object.

  • Apply the attribute ‘iloc’ to the DataFrame df.

  • Use the ‘.tolist()’ method to transform data into a python list, as our requirement is a list.

  • And lastly, use the built-in ‘print()’ function to print the output.

import pandas as pd
import numpy as np
#creating a table

student_data={
   'Name of the Student': ['Alice', 'Cassie', 'Henry', 'Steven'],
   'Age':[15,13,16,14],
   'Favourite Subject':['Math', 'Social', 'Science', 'English']
}

#Now, we will turn the student_data table into Dataframe.
print("DataFrame that we created:")
df=pd.DataFrame(student_data)
print(df)
print("Extracted values of desired Column:")
col_vals=df.iloc[:,2].tolist()
print(col_vals)

Output

DataFrame that we created:
  Name of the Student  Age Favourite Subject
0               Alice   15              Math
1              Cassie   13            Social
2               Henry   16           Science
3              Steven   14           English
Extracted values of desired Column:
['Math', 'Social', 'Science', 'English']

Method-4: using get() function

The ‘get()’ function returns the values of a column from the DataFrame or returns values of a key from a dictionary.

Syntax

 col_vals=df.get('col_name').tolist()

Example

Create a table containing Student Name, Age, and Favourite Subject and extract the values of the column “Favourite Subject” using get() function.

Algorithm

  • First import the required libraries.

  • Create a table as per the requirement.

  • Now taking df as a variable, convert the table into a DataFrame object.

  • Apply the function ‘get()’ to the DataFrame df.

  • Use the ‘.tolist()’ method to transform data into a python list, as our requirement is a list.

  • And lastly, use the built-in ‘print()’ function to print the output.

import pandas as pd
import numpy as np
#creating a table

student_data={
   'Name of the Student': ['Alice', 'Cassie', 'Henry', 'Steven'],
   'Age':[15,13,16,14],
   'Favourite Subject':['Math', 'Social', 'Science', 'English']
}

#Now, we will turn the student_data table into Dataframe.
print("DataFrame that we created:")
df=pd.DataFrame(student_data)
print(df)

#tolist() is used to convert the column values into a list.
print("Extracted values of desired Column:")
column_vals=df.get('Favourite Subject').tolist()
print(column_vals)

Output

DataFrame that we created:

DataFrame that we created:
  Name of the Student  Age Favourite Subject
0               Alice   15              Math
1              Cassie   13            Social
2               Henry   16           Science
3              Steven   14           English
Extracted values of desired Column:
['Math', 'Social', 'Science', 'English']

Conclusion

These are some of the methods to extract the list of particular column values from a table. However, we can still create many more methods to list out the column values. For example we can use a for loop to iterate through the column and print the list of column values. We can also use the ‘apply()’ method, ‘numpy.ravel()’ function, or even ‘iteritems()’ method. Above discussed methods in the article are simple and easy to understand.

Updated on: 10-Aug-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements