#----------Reference----------
# [Link]
#----------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
[Link]
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
[Link][[10]]
# Get the records of row number 10,15,17,20
[Link][[10,15,17,20]]
# Get the records for column 'actual_price'
[Link][:,['actual_price']]
# Get the records for columns 'actual_price' and 'discounted_price'
[Link][:,['actual_price','discounted_price']]
# Get the records of row number 10,15,17,20 and columns 'actual_price' and
'discounted_price'
[Link][[10,15,17,20],['actual_price','discounted_price']]
# Get the records from row number 10 to 15 and columns 'actual_price' and
'discounted_price'
[Link][10:15,['actual_price','discounted_price']]
#----------SELECT DATA USING .iloc----------
# Get rows from 1 to 15 and all columns.
# Row 15 is excluded.
[Link][1:15,:]
# Get records from 2 to 3 columns.
# Column 3 is excluded.
[Link][:,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.
[Link][1:15,1:3]
# Get records of 3 row from the tail, and columns from 2 to 4 from the DataFrame.
[Link][-3:,2:5]
# Get records for 3 row from the tail, and 1 column.
[Link][-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.
[Link][-8:-3,-4:-1]