Pandas DataFrame Index Handling in Python 🐼

Learn how to convert, set, and merge indices in pandas DataFrames with practical examples and tips.

Pandas DataFrame Index Handling in Python 🐼
Statistics Globe
329 views β€’ Sep 19, 2022
Pandas DataFrame Index Handling in Python 🐼

About this video

How to manipulate the index of a pandas DataFrame in the Python programming language. More details: https://statisticsglobe.com/index-pandas-dataframe-python
Python code of this video:

import pandas as pd # Import pandas library

data = pd.DataFrame({'x1':range(1, 10), # Create pandas DataFrame
'x2':['a', 'b', 'b', 'a', 'd', 'a', 'a', 'b', 'd'],
'x3':range(30, 21, - 1),
'x4':['x', 'z', 'z', 'y', 'y', 'z', 'x', 'z', 'x']})
print(data) # Print pandas DataFrame

data_new1 = data.copy() # Duplicate DataFrame
data_new1['index'] = data_new1.index # Convert index to column
print(data_new1) # Print updated DataFrame

data_new2 = data.set_index('x3') # Convert column to index
print(data_new2) # Print updated DataFrame

data_new3 = data_new2.reset_index() # Reindex rows of pandas DataFrame
print(data_new3) # Print updated DataFrame

data2 = pd.DataFrame({'y1':['a', 'a', 'b', 'c', 'c'], # Create second pandas DataFrame
'y2':range(30, 25, - 1),
'y3':['x', 'z', 'z', 'x', 'x']},
index = range(6, 11))
print(data2) # Print pandas DataFrame

data_new4 = pd.merge(data, # Join based on index
data2,
left_index = True,
right_index = True)
print(data_new4) # Print updated DataFrame

Follow me on Social Media:
Facebook – Statistics Globe Page: https://www.facebook.com/statisticsglobecom/
Facebook – R Programming Group for Discussions & Questions: https://www.facebook.com/groups/statisticsglobe
Facebook – Python Programming Group for Discussions & Questions: https://www.facebook.com/groups/statisticsglobepython
LinkedIn – Statistics Globe Page: https://www.linkedin.com/company/statisticsglobe/
LinkedIn – R Programming Group for Discussions & Questions: https://www.linkedin.com/groups/12555223/
LinkedIn – Python Programming Group for Discussions & Questions: https://www.linkedin.com/groups/12673534/
Twitter: https://twitter.com/JoachimSchork

Music by bensound.com

Tags and Topics

Browse our collection to discover more content in these categories.

Video Information

Views

329

Likes

6

Duration

7:02

Published

Sep 19, 2022

Related Trending Topics

LIVE TRENDS

Related trending topics. Click any trend to explore more videos.