How to Extract First & Last N Columns from pandas DataFrame in Python (2 Examples) | iloc Attribute

Published: 11 May 2022
on channel: Statistics Globe
880
8

How to return the first and last N columns from a pandas DataFrame in the Python programming language. More details: https://statisticsglobe.com/extract-f...
Python code of this video:

import pandas as pd # Load pandas library

data = pd.DataFrame({'x1':range(10, 15), # Create pandas DataFrame
'x2':range(0, 5),
'x3':['a', 'b', 'c', 'd', 'e'],
'x4':range(8, 3, - 1),
'x5':['x', 'y', 'x', 'y', 'x']})
print(data) # Print pandas DataFrame

data_first_n = data.iloc[:, :3] # Select first columns
print(data_first_n) # Print first columns

data_last_n = data.iloc[:, 3:] # Select last columns
print(data_last_n) # Print last columns

Follow me on Social Media:
Facebook – Statistics Globe Page:   / statisticsglobecom  
Facebook – Group for Discussions & Questions:   / statisticsglobe  
LinkedIn – Statistics Globe Page:   / statisticsglobe  
LinkedIn – Group for Discussions & Questions:   / 12555223  
Twitter:   / joachimschork  

Music by bensound.com