Tutorials  /  Python

Effective Methods for Creating Dataframe Subsets in Python

Ccentron Redaktion · May 2024 ·4 min read ·Python, Tutorial

Learn in our latest blog post how to effortlessly create subsets using Python Pandas Dataframes. Explore three different methods for efficient data filtering and manipulation.

A Python Dataframe? What is it, anyway?

The Python Pandas modules provide us with two data structures: Series and Dataframe, for storing values. A Dataframe is a data structure that stores data in the form of a matrix, i.e., rows and columns. With a Dataframe, we can create subsets in various ways and access them:

  • Accessing data by rows as subsets
  • Retrieving data by columns as subsets
  • Accessing specific data from specific rows and columns as subsets
VM

Matching infrastructure at centron

Scripts and services eventually need an environment that keeps running: ccloud³ VMs from €3.12 per month, billed by the hour. Rent a cloud server →

Creating a Dataframe to Work With!

Python
import pandas as pd 
data = {"Roll-num": [10,20,30,40,50,60,70], "Age":[12,14,13,12,14,13,15], "NAME":['John','Camili','Rheana','Joseph','Amanti','Alexa','Siri']}
block = pd.DataFrame(data)
print("Original Dataframe:\n")
print(block)
Code
 Original Dataframe:
   Roll-num  Age    NAME
0        10   12    John
1        20   14  Camili
2        30   13  Rheana
3        40   12  Joseph
4        50   14  Amanti
5        60   13   Alexa
6        70   15    Siri

Here, we created a Dataframe using the pandas.DataFrame() method. We will use this created dataset throughout this article.

Creating Subsets of a Python Dataframe Using the loc() Function

The Python loc() function allows us to create subsets of a Dataframe based on a specific row, column, or a combination of both. The loc() function operates based on labels, meaning we need to provide it with the label of the row/column to create custom subsets.

Example 1: Extracting data of specific rows from a Dataframe

Code
block.loc[[0,1,3]]
Code
   Roll-num  Age    NAME
0        10   12    John
1        20   14  Camili
3        40   12  Joseph

Example 2: Creating subsets of rows using slicing

Code
block.loc[0:3]
Code
   Roll-num  Age    NAME
0        10   12    John
1        20   14  Camili
2        30   13  Rheana
3        40   12  Joseph

Example 3: Creating subsets of specific columns with labels

Code
block.loc[0:2,['Age','NAME']]
Code
   Age    NAME
0   12    John
1   14  Camili
2   13  Rheana

Using the Python iloc() Function to Create Subsets of a Dataframe

The Python iloc() function allows us to select subsets of rows and columns based on index values. Unlike the loc() function, which operates based on labels, the iloc() function operates based on index values. We can create subsets of a Python Dataframe's data by specifying the index numbers of rows and columns.

Example:

Code
block.iloc[[0,1,3,6],[0,2]]
Code
   Roll-num    NAME
0        10    John
1        20  Camili
3        40  Joseph
6        70    Siri

Using the Index Operator to Create Subsets of a Dataframe

We can use the index operator, square brackets, to easily create subsets of the data.

Example:

Code
block[['Age','NAME']]
Code
   Age    NAME
0   12    John
1   14  Camili
2   13  Rheana
3   12  Joseph
4   14  Amanti
5   13   Alexa
6   15    Siri

Conclusion

With this, we've reached the end of this topic. Please feel free to leave a comment below if you have any questions. Stay tuned for more Python articles, and until then, happy learning!

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Python
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren