Menu Close

How to Access a Column in DataFrame

How to access a column in Pandas DataFrame

Hello Python, Programmers, In this article, we are going to learn all about how to access a column in DataFrame using Pandas. In our previous tutorial, we have a lot of articles on Pandas with the help of the examples but in this guide, we will give you a completed guide to select a column from Pandas DataFrame with the help of the examples.

What is the DataFrame in Python?

Python provides a popular library Pandas that allows creating DataFrame.In Python, DataFrame is a two-dimensional array that builds with a combination of rows and columns. You can see the below example to understand Python DataFrame.

Example:

In this example, we convert CSV file data to Pandas DataFrame using the Pandas read_csv() method.


import pandas as pd

df = pd.read_csv("sample.csv")
# print
print(df)

Output

            Name  Age           Occupation                   Skills
0  Vishvajit Rao   23            Developer                   Python
1           John   33  Front End Developer                  Angular
2       Harshita   21               Tester                 Selenium
3          Mohak   30           Full Stack  Python, React and MySQL
4           John   35       Data Scientist                  Python

How to access a column in DataFrame

So let’s see how can we select a column from Pandas DataFrame with the help of the proper examples. Python DataFrame provides various methods to access columns in Pandas DataFrame.

Using loc

Python DataFrame provides a loc property that is capable of accessing columns and rows in DataFrame.Let’s see how.DataFrame loc property takes various types of inputs which are described below.

DataFrame loc property takes these inputs:

  • A list of labels, e.g. [‘a’, ‘b’, ‘c’]
  • A single label e.g. ‘1’ or ‘a’
  • A slice objects with labels e.g. [‘a’: ‘f’]
  • A boolean array with the same length as the axes being sliced e.g. [False, True, False]

Example: How to get column values in Pandas

In this example, we are going to select the Skills column from the DataFrame.


import pandas as pd

df = pd.read_csv("sample.csv")

# access Skills column
skill = df.loc[:, "Skills"]

# print 
print(skill)

Output

0                     Python
1                    Angular
2                   Selenium
3    Python, React and MySQL
4                    Python
Name: Skills, dtype: object

Explanation:

  • Load sample.csv file and convert into DataFrame.
  • Use DataFrame df loc properties.
  • (:) specify the rows you like to index.
  • Skills represent the name of the column in DataFrame.

Using iloc Property

DataFrame iloc property is also capable of accessing a column in DataFrame, here we will pass the integer value that represents the index of the column. Let’s see how can we do that.

Example: How to access a column in DataFrame Python

In this example, we are going to select the Name column from the Pandas DataFrame.The Name column represents the index 0 in DataFrame.


import pandas as pd

df = pd.read_csv("sample.csv")

# access Skills column
values = df.iloc[:, [0]]
print(values)

Output

            Name
0  Vishvajit Rao
1           John
2       Harshita
3          Mohak
4           John

By Using Column Name

We can also select a particular column in Pandas DataFrame using the column name, Here we are going to select the Occupation column from the Pandas DataFrame.

Example: How to Access a Column in DataFrame


import pandas as pd

df = pd.read_csv("sample.csv")

# access Skills column
skill = df['Occupation']
print(skill)

Output

0                     Python
1                    Angular
2                   Selenium
3    Python, React and MySQL
4                    Python

Select Multiple Columns in Pandas DataFrame

In the above example, we have seen select only a single column from the DataFrame but using that functionality we can select multiple columns in pandas DataFrame as well. Let’s see.

Example: How to select columns in Pandas


import pandas as pd

df = pd.read_csv("sample.csv")

# access Skills and Occupation columns column
values = df.loc[:, ["Skills", "Occupation"]]
print(values)

Output

                    Skills           Occupation
0                   Python            Developer
1                  Angular  Front End Developer
2                 Selenium               Tester
3  Python, React and MySQL           Full Stack
4                  Python        Data Scientist

Example: How to select multiple columns in Pandas DataFrame


import pandas as pd

df = pd.read_csv("sample.csv")

# access Skills column
values = df[['Name', 'Occupation']]
print(values)

Output

            Name           Occupation
0  Vishvajit Rao            Developer
1           John  Front End Developer
2       Harshita               Tester
3          Mohak           Full Stack
4           John       Data Scientist

Example: How to select multiple columns in Pandas DataFrame


import pandas as pd

df = pd.read_csv("sample.csv")

# access Skills column
values = df.iloc[:, [0,1,2]]
print(values)

Output

            Name  Age           Occupation
0  Vishvajit Rao   23            Developer
1           John   33  Front End Developer
2       Harshita   21               Tester
3          Mohak   30           Full Stack
4           John   35       Data Scientist

Conclusion

So, In this article, we have seen all about how to access a column in DataFrame and also we can see how to access multiple columns in DataFrame with the help of proper examples.
I hope this article will help you, If you like this article, please share a keep visit for this type of knowledgeable information.

Related articles:

Thanks for your valuable time…

Previous Article
Next Article

Related Posts