I have a pandas Series with Multi-index as follows:
category_1 number
A 0 1.764052
1 0.400157
2 0.978738
3 2.240893
4 1.867558
C 0 -0.977278
1 0.950088
2 -0.151357
3 -0.103219
4 0.410599
It is generated from this code:
import pandas as pd
import numpy as np
idx = pd.MultiIndex.from_product([['A','C'],range(5)], names=['category_1','number'])
np.random.seed(0)
s = pd.Series(index=idx, data = np.random.randn(len(idx)))
I would like to add another level, called category_2
to the index with a fixed value (ie D
) to have the following result:
category_1 category_2 number
A D 0 1.764052
1 0.400157
2 0.978738
3 2.240893
4 1.867558
C D 0 -0.977278
1 0.950088
2 -0.151357
3 -0.103219
4 0.410599
I have been using this hacky way to do this:
df =s.to_frame('dummy')
df['category_2'] = 'D'
df.set_index('category_2', append = True, inplace = True)
df = df.reorder_levels([0,2,1])
res = df['dummy']
Is there a better (more succinct/pythonic) way to add a level with fixed value to the existing levels on a pandas Series/DataFrame?
You need create new MultiIndex
and then replace old one:
#change multiindex
new_index = list(zip(s.index.get_level_values('category_1'),
['D'] * len(s.index),
s.index.get_level_values('number')))
print (new_index)
[('A', 'D', 0), ('A', 'D', 1),
('A', 'D', 2), ('A', 'D', 3),
('A', 'D', 4), ('C', 'D', 0),
('C', 'D', 1), ('C', 'D', 2),
('C', 'D', 3), ('C', 'D', 4)]
s.index = pd.MultiIndex.from_tuples(new_index,
names=['category_1','category_2','number'])
print (s)
category_1 category_2 number
A D 0 1.764052
1 0.400157
2 0.978738
3 2.240893
4 1.867558
C D 0 -0.977278
1 0.950088
2 -0.151357
3 -0.103219
4 0.410599
dtype: float64
Another nice solution with MultiIndex.from_product
- a bit changed comment :
s.index = pd.MultiIndex.from_product([s.index.levels[0],
['D'],
s.index.levels[1]], names= ['c1','c2','number'])
print (s)
c1 c2 number
A D 0 1.764052
1 0.400157
2 0.978738
3 2.240893
4 1.867558
C D 0 -0.977278
1 0.950088
2 -0.151357
3 -0.103219
4 0.410599
dtype: float64
Or:
s.index = pd.MultiIndex.from_product([s.index.get_level_values('category_1').unique(),
['D'],
s.index.get_level_values('number').unique()],
names= ['c1','c2','number'])
print (s)
c1 c2 number
A D 0 1.764052
1 0.400157
2 0.978738
3 2.240893
4 1.867558
C D 0 -0.977278
1 0.950088
2 -0.151357
3 -0.103219
4 0.410599
dtype: float64
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.