Pandas Data Series: Change the order of index of a given series
Pandas: Data Series Exercise-14 with Solution
Write a Pandas program to change the order of index of a given series.
Sample Solution :
Python Code :
import pandas as pd s = pd.Series(data = [1,2,3,4,5], index = ['A', 'B', 'C','D','E']) print("Original Data Series:") print(s) s = s.reindex(index = ['B','A','C','D','E']) print("Data Series after changing the order of index:") print(s)
Original Data Series: A 1 B 2 C 3 D 4 E 5 dtype: int64 Data Series after changing the order of index: B 2 A 1 C 3 D 4 E 5 dtype: int64
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Pandas program to create a subset of a given series based on value and condition.
Next: Write a Pandas program to create the mean and standard deviation of the data of a given Series.
What is the difficulty level of this exercise?
Test your Python skills with w3resource's quiz
Python: Tips of the Day
Returns a list with n elements removed from the beginning
def tips_take(itr, n = 1): return itr[:n] print(tips_take([1, 2, 3], 5)) print(tips_take([1, 2, 3], 0))
[1, 2, 3] 
- New Content published on w3resource:
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework