codingstreets
Search
Close this search box.
introduction-to-pandas-dataframe-in-python

Introduction to Pandas DataFrame in Python

In This Article, You Will Learn About Python Pandas DataFrame

Python Pandas DataFrame – Before moving ahead, let’s know about Python Introduction

Table of Contents

DataFrame

A Pandas DataFrame is a 2D data structure, similar to an array with two dimensions, or a table, with columns and rows.

Example –  Creating a DataFrame from two Series.

				
					import pandas as pd

info = {
  "Name": ["A", "B", "C"],
  "Score": [90, 89, 98]
}

data = pd.DataFrame(info)

print(data)

				
			

As shown clearly, it returned 2D data in tabular form.

Locate Row

You can see that the DataFrame functions like a table that has columns and rows. Pandas utilize their loc attribute to return one or more specific row(s).

Example – Locating the 2d row from 2D data.

				
					import pandas as pd

info = {
  "Name": ["A", "B", "C"],
  "Score": [90, 89, 98]
}

data = pd.DataFrame(info)

print(data.loc[2])

				
			

As shown above, it returned the specified data i.e., 2nd row’s data after using default row’s name as index argument.

Example – Locating the multiple rows.

				
					import pandas as pd

info = {
  "Name": ["A", "B", "C"],
  "Score": [90, 89, 98]
}

data = pd.DataFrame(info)

print(data.loc[[0,2]])

				
			

As a result, it returned specified data within the range.

Note: Using the parenthesis — [  ] we can get the data in DataFrame.

Name Indexes

By using index argument, we can put name to our own indexes.

Example – Creating a list of name to give name to rows.

				
					import pandas as pd

info = {
  "Name": ["A", "B", "C"],
  "Score": [90, 89, 98]
}

data = pd.DataFrame(info, index=["Class-X", "Class-XI", "Class-XII"])

print(data)
				
			

It has been noticed, it returned each row has been defined with their row name.

Locate Named Indexes

Using the named index of the loc attribute to retrieve the specified row(s).

Example – Locating the 2d row from 2D data.

				
					import pandas as pd

info = {
  "Name": ["A", "B", "C"],
  "Score": [90, 89, 98]
}

data = pd.DataFrame(info)

print(data.loc["Class-XII"])

				
			

As shown above, it returned specified data (2D row) from 2D data by using row name as index argument.

Load Files

If your data is stored in a file, then Pandas can load it to DataFrame.

Example – Loading a file CSV file into a DataFrame.

				
					import pandas as pd

file = pd.read_csv('myfile.csv')

print(file) 

				
			

As shown above, it loaded given file with the help of Pandas read_csv function.

If you find anything incorrect in the above-discussed topic and have any further questions, please comment below.

Recent Post

Popular Post

Top Articles

Archives
Categories

Share on