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, 2019 in Python by Hannah
• 18,060 points
83 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, 2019 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, 2019 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,250 points
153 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, 2019 in Python by Mohammad
• 3,230 points
174 views
0 votes
1 answer

How to change the file name of an uploaded file in Django?

Hello @kartik, The basic way is import os os.rename('a.txt', 'b.html') For ...READ MORE

answered 1 day ago in Python by Niroj
• 32,440 points
11 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
7,160 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,770 points
254 views
0 votes
1 answer

Need help checking the validity of an image file in Python

I went through the Python documentation and ...READ MORE

answered Jan 18, 2019 in Python by Nymeria
• 3,540 points
163 views
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, 2019 in Python by Pavan
703 views
0 votes
1 answer
0 votes
1 answer

How to create Pandas series from dictionary?

Here's a sample script: import pandas as pd import ...READ MORE

answered Apr 1, 2019 in Python by Prateek
149 views