0% found this document useful (0 votes)
53 views

Module 05.0 - PA - Pandas - DataFrame - Select - Data

The document discusses various methods for selecting data from a pandas DataFrame using indexing, slicing, and .loc and .iloc properties. It provides examples of selecting rows and columns using positive and negative indexing, slicing ranges with and without frequency, and multi-axis selection using .loc and .iloc to select by label or integer position. These methods allow precise selection of subsets of data from DataFrames for further analysis or processing.

Uploaded by

RAHUL DUTTA
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Module 05.0 - PA - Pandas - DataFrame - Select - Data

The document discusses various methods for selecting data from a pandas DataFrame using indexing, slicing, and .loc and .iloc properties. It provides examples of selecting rows and columns using positive and negative indexing, slicing ranges with and without frequency, and multi-axis selection using .loc and .iloc to select by label or integer position. These methods allow precise selection of subsets of data from DataFrames for further analysis or processing.

Uploaded by

RAHUL DUTTA
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#----------Reference----------

# https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html

#----------GET DATA FROM FILE----------

# Import pandas library


import pandas as pd

# Get the file path


sales_data = 'E:/Courses/Python_for_Analytics/Data/mobile_price_index.csv'

# Read the file and save as DataFrame


df = pd.read_csv(sales_data, header=0)

#print('Original DataFrame of Input File \n ', df)

#----------SELECT DATA USING COLUMN LABELS----------

# View column labels


df.columns
Segment

# View column 'mobile_name' from the DataFrame


df['mobile_name']

# View column 'mobile_name' from the DataFrame


df.mobile_name
new

# Create a new dataframe of columns


df_new = df[['mobile_name', 'actual_price', 'discounted_price']]
df_new

#----------SLICING RANGE (ROWS) WITH POSITIVE INDEX----------

# Get records of row number 0 from the DataFrame. Row 3 is excluded.


df[:3]

# Get records from row number 10 onwards till end.


df[10:]

# Get records of row number 0 from the DataFrame. Row 1 is excluded.


df[0:2]

# Get records from row number 10 to 14 from the DataFrame. Row 15 is excluded.
df[10:15]

#----------SLICING RANGE (ROWS) WITH NEGATIVE INDEX----------

# Get the last record.


df[-1:]

# Get records from row number 0 to 3 from the the tail of the dataframe. Row -3 is
excluded.
df[-3:]

# Get records from row number 0 to 4 from the the head of the dataframe.
df[:-20]

# Get records from row number 1 to 3 from the the tail of the dataframe. Row -3 is
excluded.
df[-5:-1]

#----------SLICING RANGE (ROWS) WITH FREQUENCY----------

# Get all records at frequency 5


df[::5]

# Get records from 10th row at frequency 2


df[10::2]

# Get all records at frequency (negative) -3


df[::-3]

# Get records from index 0 to 10 at frequency (negative) -3


df[10::-3]

#----------SELECT DATA USING .loc----------

# Get the records of row number 10


df.loc[[10]]

# Get the records of row number 10,15,17,20


df.loc[[10,15,17,20]]

# Get the records for column 'actual_price'


df.loc[:,['actual_price']]

# Get the records for columns 'actual_price' and 'discounted_price'


df.loc[:,['actual_price','discounted_price']]

# Get the records of row number 10,15,17,20 and columns 'actual_price' and
'discounted_price'
df.loc[[10,15,17,20],['actual_price','discounted_price']]

# Get the records from row number 10 to 15 and columns 'actual_price' and
'discounted_price'
df.loc[10:15,['actual_price','discounted_price']]

#----------SELECT DATA USING .iloc----------

# Get rows from 1 to 15 and all columns.


# Row 15 is excluded.
df.iloc[1:15,:]

# Get records from 2 to 3 columns.


# Column 3 is excluded.
df.iloc[:,2:3]

# Get records from row number 1 to 15, and columns from 1 to 3 from the DataFrame.
# Row 15 and column 3 are excluded.
df.iloc[1:15,1:3]

# Get records of 3 row from the tail, and columns from 2 to 4 from the DataFrame.
df.iloc[-3:,2:5]

# Get records for 3 row from the tail, and 1 column.


df.iloc[-3:,-1:]

# Get records from row number 3 to 8 from the tail, and columns from 1 to 3 from
the tail.
# 3rd will be considered.
df.iloc[-8:-3,-4:-1]

You might also like