Logo

dev-resources.site

for different kinds of informations.

Pandas: Conversion using loc and iloc

Published at
1/6/2025
Categories
pandas
data
Author
vidyasagarmsc
Categories
2 categories in total
pandas
open
data
open
Author
13 person written this
vidyasagarmsc
open
Pandas: Conversion using loc and iloc

Pandas is a powerful Python library used for data manipulation and analysis.

Created by Wes McKinney in 2008, it provides data structures and functions for working with structured data efficiently. Pandas allows users to analyze big data, clean messy datasets, and derive meaningful insights.


Photo by Kevin Canlas on Unsplash

Key Features of Pandas

  1. Data Structures: Pandas introduces two primary data structures:
  • Series: A one-dimensional labeled array
  • DataFrame: A two-dimensional labeled data structure with columns of potentially different types
  1. Data Manipulation: Pandas offers functions for analyzing, cleaning, exploring, and manipulating data.

  2. Data Analysis: It enables users to perform complex operations like correlation analysis, grouping, and statistical calculations.

  3. Data Visualization: Pandas integrates well with other libraries to create insightful visualizations.

Practical Examples

Indexing with loc

The loc function enables label-based indexing in DataFrames, allowing precise data selection:

import pandas as pd

# Create a sample DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}, index=['x', 'y', 'z'])

# Select rows with label 'y' and 'z', and columns 'A' and 'C'
result = df.loc[['y', 'z'], ['A', 'C']]
print(result)
Enter fullscreen mode Exit fullscreen mode

The iloc function provides integer-based indexing for DataFrame selection:

import pandas as pd

# Create a sample DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})

# Select rows 0 and 2, and columns 1 and 2
result = df.iloc[[0, 2], [1, 2]]
print(result)
Enter fullscreen mode Exit fullscreen mode

Date Conversion with to_datetime

The to_datetimefunction transforms various date formats into standardized datetime objects:

import pandas as pd

# Convert string to datetime
date_string = "2023-09-17 14:30:00"
dt_object = pd.to_datetime(date_string)
print(dt_object)

# Convert multiple date strings
date_series = pd.Series(['20200101', '20200201', '20200301'])
dt_series = pd.to_datetime(date_series, format='%Y%m%d')
print(dt_series)
Enter fullscreen mode Exit fullscreen mode

Output:

2023-09-17 14:30:00
0 2020-01-01
1 2020-02-01
2 2020-03-01
dtype: datetime64[ns]
Enter fullscreen mode Exit fullscreen mode

Pandas simplifies data manipulation tasks, making it an essential tool for data scientists and analysts. Its versatile functions like loc, iloc, and to_datetime provide powerful ways to interact with and transform data, enabling efficient data processing and analysis in Python.

Something to consider while using loc or iloc

Let’s convert the object column date to datetime using loc

import pandas as pd

df = pd.DataFrame({'date': ['2023-01-01', '2023-02-15', '2023-03-31']})

df.loc[:, 'date'] = pd.to_datetime(df.loc[:, 'date'])

print(df)
print(df.dtypes)

Output: 

   date
0 2023-01-01 00:00:00
1 2023-02-15 00:00:00
2 2023-03-31 00:00:00
date object
dtype: object
Enter fullscreen mode Exit fullscreen mode

If you observe, the dtype is object not datetime64[ns]. If you try to extract the date using df['date'].dt.date

You will see an error as the conversion was not successful.

Traceback (most recent call last):
  File "/HelloWorld.py", line 11, in <module>
    print(df.dt.date)
          ^^^^^
  File "/usr/local/lib/python3.12/dist-packages/pandas/core/generic.py", line 6299, in __getattr__
    return object. __getattribute__ (self, name)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'DataFrame' object has no attribute 'dt'. Did you mean: 'at'?
Enter fullscreen mode Exit fullscreen mode

The reason lies in the changes made in version 2.x.x of Pandas.

From What’s new in 2.0.0 (April 3, 2023):

Changed behavior in setting values with df.loc[:, foo] = bar or df.iloc[:, foo] = bar, these now always attempt to set values inplace before falling back to casting (GH 45333)

How to overcome:

The best way to address this issue is to either avoid using loc or iloc or as suggested on the Pandas documentation use DataFrame.__setitem__()

df = pd.DataFrame({'date': ['2023-01-01', '2023-02-15', '2023-03-31']})

df['date'] = pd.to_datetime(df.loc[:, 'date'])

print(df)
print(df.dtypes)

print(df['date'].dt.date)
Enter fullscreen mode Exit fullscreen mode

Additional read:

https://stackoverflow.com/questions/76766136/pandas-pd-to-datetime-assigns-object-dtype-instead-of-datetime64ns


data Article's
30 articles in total
Favicon
Why Schema Compatibility Matters
Favicon
Massively Scalable Processing & Massively Parallel Processing
Favicon
Interactive Python plots: Getting started and best packages
Favicon
Dados da Web
Favicon
Google and Anthropic are working on AI agents - so I made an open source alternative
Favicon
Efficiently Deleting Millions of Objects in Amazon S3 Using Lifecycle Policy
Favicon
Elon Musk agrees that we’ve exhausted AI training data
Favicon
Data Analysis Trends for Beginners: What's Popular in 2025?
Favicon
AI and Automation in Data Analytics: Tools, Techniques, and Challenges
Favicon
High-Demand Tools and Platforms for Freelance Data Analysts in 2025
Favicon
Using proxy IP for data cleaning and preprocessing
Favicon
Quickly and easily filter your Amazon CloudWatch logs using Logs Insights
Favicon
A Guide to Manage Access in SQL - GRANT, REVOKE, and Access Control
Favicon
Weekly Updates - Jan 10, 2025
Favicon
Solving the Logistics Puzzle: How Geospatial Data Visualization Optimizes Delivery and Transportation
Favicon
🔍 Handling Missing Data in Python for Real-World Applications
Favicon
A Quick Guide to SQL Data Modification Commands with Examples
Favicon
chkbit checks for data corruption
Favicon
Enterprise Data Architecture and Modeling: Key Practices and Trends
Favicon
What kind of Data Team should I join?
Favicon
Proxy IP and crawler anomaly detection make data collection more stable and efficient
Favicon
What data can crawlers collect through HTTP proxy IP?
Favicon
Pandas: Conversion using loc and iloc
Favicon
The Only Thing Successful Entrepreneurs Care About..
Favicon
Session management of proxy IP in crawlers
Favicon
The Unofficial Snowflake Monthly Release Notes: December 2024
Favicon
A Closer Look at the Top 5 Data Protection Software in 2024
Favicon
The beginning of my journey
Favicon
Hi! Just finished my first blogpost here, with some test of DuckDB and OSM data. Public notebook attached! ;)
Favicon
How Data Analytics in the Cloud Can Level Up Your App

Featured ones: