简体   繁体   中英

How to reset only first level of MultiIndex in pandas

I've got a DataFrame like below:

ex = pd.DataFrame({'speed': {(1252540, 0): 0.0,
  (1252540, 1): 0.0,    
  (1252540, 2): 0.0,
  (1252541, 0): 0.0,
  (1252541, 1): 0.0,
  (1252541, 2): 0.0,
  (1252543, 0): 0.0,
  (1252543, 1): 0.0,
  (1252543, 2): 0.0,
  (1252544, 0): 0.0,
  (1252544, 1): 0.0,
  (1252544, 2): 0.0,
  (1252545, 0): 0.0,
  (1252545, 1): 0.0,
  (1252545, 2): 0.0,
  (1252546, 3): 0.0,
  (1252546, 4): 0.0,
  (1252546, 5): 0.0,
  (1252547, 3): 0.0,
  (1252547, 4): 0.0},
 'unknown': {(1252540, 0): np.nan,
  (1252540, 1): np.nan,
  (1252540, 2): np.nan,
  (1252541, 0): np.nan,
  (1252541, 1): np.nan,
  (1252541, 2): np.nan,
  (1252543, 0): np.nan,
  (1252543, 1): np.nan,
  (1252543, 2): np.nan,
  (1252544, 0): np.nan,
  (1252544, 1): np.nan,
  (1252544, 2): np.nan,
  (1252545, 0): np.nan,
  (1252545, 1): np.nan,
  (1252545, 2): np.nan,
  (1252546, 3): np.nan,
  (1252546, 4): np.nan,
  (1252546, 5): np.nan,
  (1252547, 3): np.nan,
  (1252547, 4): np.nan}})
ex.index.names = ['id', 'id2']

I'd like to set a first level of MultiIndex to (0, 0, 0, 1, 1, 1, 2, 2, 2, ...) so that each new value in level 0 is assigned with next integer. Normally, I could do simple shift with something like:

idx = ex.index.get_level_values(0).to_numeric()
idx -= idx.min()

but as you can see, some values ( 1252542 ) may be missing from original index, while there shouldn't be any gap in new indexing. How can I accomplish that? If I could preserve the mapping (like 1252540 -> 0, 1252541 -> 1, 1252543 -> 2... ), possibly in a form of dict, it'd great, but it's not mandatory.

Let me know if this helps:

indices = ex.index.get_level_values('id').unique().sort_values()

dict = {}

for key,value in (zip(indices,range(0,len(indices)))):
    dict[key] = value

ex.rename(index=dict)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM