![]() |
Slicing column values in Pandas is a crucial operation in data manipulation and analysis. Pandas, a powerful Python library, provides various methods to slice and extract specific data from DataFrames. This article will delve into the different techniques for slicing column values, highlighting their syntax, examples, and applications. Table of Content Introduction to Pandas DataFrameA Pandas DataFrame is a two-dimensional, size-mutable, and heterogeneous tabular data structure with labeled axes (rows and columns). It is similar to a spreadsheet or SQL table and is one of the most commonly used data structures in data analysis. To get started, let’s create a simple DataFrame:
Output: Name Age City
0 Alice 25 New York
1 Bob 30 Los Angeles
2 Charlie 35 Chicago Slicing Column Values using Indexing1. Positional Indexing with ilocThe iloc function is used for positional indexing, which allows you to slice data based on numerical positions.
Output: 0 Alice
1 Bob
Name: Name, dtype: object 2. Label-based Indexing with locThe loc function is used for label-based indexing, which allows you to slice data based on row and column labels.
Output: 0 Alice
1 Bob
Name: Name, dtype: object Slicing Column Values using String Methods1. Accessing SubstringsYou can access substrings of column values using the str accessor.
Output: Name Age City Name_Short
0 Alice 25 New York Ali
1 Bob 30 Los Angeles Bob
2 Charlie 35 Chicago Cha 2. Using Regular ExpressionsRegular expressions can be used for more complex slicing.
Output: Name Age City Name_Short City_Digits
0 Alice 25 New York Ali NaN
1 Bob 30 Los Angeles Bob NaN
2 Charlie 35 Chicago Cha NaN Slicing Column Values in Pandas : Advanced Techniques1. Slicing with apply and lambdaThe apply function combined with a lambda function provides a flexible way to slice column values.
Output: Name Age City Name_Short City_Digits City_First_Letter
0 Alice 25 New York Ali NaN N
1 Bob 30 Los Angeles Bob NaN L
2 Charlie 35 Chicago Cha NaN C 2. Using str.split for Complex SlicingThe str.split method splits strings based on a specified delimiter and returns a list. You can then slice these lists to extract specific parts.
Output: Name Age City Name_Short City_Digits City_First_Letter \
0 Alice 25 New York Ali NaN N
1 Bob 30 Los Angeles Bob NaN L
2 Charlie 35 Chicago Cha NaN C
Name_Split
0 A
1 Bob
2 Char Practical Examples: Slicing Columns in a Real-World DatasetExample 1: Analyzing Titanic Passenger DataLet’s consider a dataset of Titanic passengers:
Output: PassengerId Survived Pclass ... Fare Cabin Embarked
0 1 0 3 ... 7.2500 NaN S
1 2 1 1 ... 71.2833 C85 C
2 3 1 3 ... 7.9250 NaN S
3 4 1 1 ... 53.1000 C123 S
4 5 0 3 ... 8.0500 NaN S 1. Slicing Specific Columns:
Output: Name Age Sex
0 Braund, Mr. Owen Harris 22.0 male
1 Cumings, Mrs. John Bradley (Florence Briggs Th... 38.0 female
2 Heikkinen, Miss. Laina 26.0 female
3 Futrelle, Mrs. Jacques Heath (Lily May Peel) 35.0 female
4 Allen, Mr. William Henry 35.0 male 2. Slicing Columns by Index:
Output: Survived Pclass Name
0 0 3 Braund, Mr. Owen Harris
1 1 1 Cumings, Mrs. John Bradley (Florence Briggs Thayer)
2 1 3 Heikkinen, Miss. Laina
3 1 1 Futrelle, Mrs. Jacques Heath (Lily May Peel)
4 0 3 Allen, Mr. William Henry Example 2: Slicing Substrings in a Product Codes DatasetConsider a dataset with product codes:
Output: ProductCode Price
0 A12345 100
1 B67890 150
2 C54321 200
3 D98765 250 1. Extracting Product Category:
Output: ProductCode Price Category
0 A12345 100 A
1 B67890 150 B
2 C54321 200 C
3 D98765 250 D 2. Extracting Product Number:
Output: ProductCode Price Category ProductNumber
0 A12345 100 A 12345
1 B67890 150 B 67890
2 C54321 200 C 54321
3 D98765 250 D 98765 ConclusionSlicing column values in Pandas is a fundamental skill for data manipulation and analysis. Whether you need to slice entire columns or extract substrings from column values, Pandas provides versatile methods to accomplish these tasks. By mastering these techniques, you can efficiently preprocess and analyze your data, making your data analysis workflows more effective and streamlined. |
Reffered: https://www.geeksforgeeks.org
AI ML DS |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 16 |