How To Split Data In Python Pandas

Splitting data is a common task in data analysis. In Python’s Pandas library, splitting the data can be done using various methods. In this tutorial, we will explore how to split data in Python Pandas.

Step 1: Importing the necessary libraries

Before we start splitting the data, we need to import the necessary libraries. In this case, we will import Pandas library.

Step 2: Creating a DataFrame

Once the necessary library is imported, let us create a DataFrame. We will use the following code to create a sample DataFrame.

Step 3: Splitting data based on columns

Splitting the data based on columns can be done using the loc method. The loc method is used to access a group of rows and columns in the DataFrame.

Suppose we want to split the data based on the Age column. We can use the following code.

This will split the data into two new DataFrames based on the age column. The first DataFrame will contain data for people younger than 30, and the second DataFrame will contain data for people 30 and older.

Step 4: Splitting data randomly

Splitting data randomly can be done using the sample method. The sample method is used to randomly sample rows from a DataFrame.

Suppose we want to split the data into two new DataFrames randomly. We can use the following code.

This code splits the data into two new DataFrames. The first DataFrame will contain 70% of the original data, and the second DataFrame will contain the remaining 30% of the original data.

Conclusion:

In this tutorial, we learned how to split data in Python Pandas using different methods. We saw how to split data based on columns and also how to split data randomly. By splitting data, we can analyze and process it easily. Pandas is a powerful library that makes data analysis in Python easy and efficient.

   Name  Age Gender
0   Mark   22      M
1   John   34      M
2  Sarah   29      F
3    Mia   26      F
4    Kim   40      F
   Name  Age Gender
0   Mark   22      M
3    Mia   26      F
2  Sarah   29      F

    Name  Age Gender
1   John   34      M
4    Kim   40      F

Full Code: