Change the default indexing of an array in pandas

0 votes
I have a series object in python, created using pandas. The default indexing is 0,1,2,3...

I want the default indexing to be a,b,c,d,...

How do I do this?
Nov 18 in Python by Hannah
• 17,060 points
25 views

2 answers to this question.

0 votes

Hey @Hannah, Hope you are doing good. Changing the indexing of an array is pretty simple. While initializing an array, you just have to add an additional parameter called index.

variable = pd.Series([1,2,3,4]index=['a','b','c','d'])
answered Nov 18 by Eric
+1 vote
df = pd.DataFrame({'month': [1, 4, 7, 10],
                   'year': [2012, 2014, 2013, 2014],
                   'sale': [55, 40, 84, 31]})
print(df)
print(df.set_index('month'))
print(df.set_index(['year', 'month']))

first print stmt gives default index,next print stmt gives default index change to month,third print stmt gives year month as index
answered Nov 27 by nagendra reddy
Hello, @Nagendra reddy! Please do sign in at Edureka Community and post answers and comments to gain points and rewards.

Related Questions In Python

0 votes
1 answer

Is arr.__len__() the preferred way to get the length of an array in Python?

my_list = [1,2,3,4,5,6,7] len(my_list) # 7 The same works for ...READ MORE

answered Oct 8, 2018 in Python by SDeb
• 13,210 points
81 views
0 votes
1 answer

how do I check the length of an array in a python program?

lets say we have a list mylist = ...READ MORE

answered Mar 12 in Python by Mohammad
• 3,170 points
69 views
+1 vote
4 answers

Count the frequency of an item in a python list

To count the number of appearances: from collections ...READ MORE

answered Oct 18, 2018 in Python by tinitales
2,005 views
0 votes
1 answer

How can I find out the index of an element from row and column in Python?

You probably want to use np.ravel_multi_index: [code] import numpy ...READ MORE

answered Apr 16, 2018 in Python by charlie_brown
• 7,720 points
107 views
0 votes
1 answer
0 votes
1 answer

How to create Pandas series from numpy array?

Hi. Refer to the below command: import pandas ...READ MORE

answered Apr 1 in Python by Pavan
251 views
0 votes
1 answer
0 votes
1 answer