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,520 points
1,145 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 28, 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,300 points
947 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
1,200 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 Jul 3, 2020 in Python by Niroj
• 82,840 points
4,630 views
+1 vote
1 answer

How to change the order of DataFrame columns in pandas?

Hi@akhtar, You can rearrange a DataFrame object by ...READ MORE

answered Oct 20, 2020 in Python by MD
• 95,460 points
860 views
+1 vote
8 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
35,999 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
2,340 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
3,506 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
2,318 views
0 votes
2 answers
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP