简体   繁体   English

如何向 pandas pivot 表添加新索引?

[英]How to add a new index to a pandas pivot table?

I have a dataframe that looks like this:我有一个看起来像这样的 dataframe:

>>> df
                                   neg      pos
time                level                      
2020-06-26 06:59:00 19.070000      4.0      0.0
                    19.080002      0.0      1.0
                    19.090000      9.0      3.0
                    19.099998      1.0      8.0
                    19.110001      0.0     14.0
                    19.119999      0.0      4.0
2020-06-26 07:00:00 19.060001      5.0     10.0
                    19.070000      9.0      4.0
                    19.080002      7.0      7.0
...
df.to_dict()

{'neg': {('2020-06-26 06:59:00', 19.07): 4.0,
  ('2020-06-26 06:59:00', 19.080002): 0.0,
  ('2020-06-26 06:59:00', 19.09): 9.0,
  ('2020-06-26 06:59:00', 19.099998): 1.0,
  ('2020-06-26 06:59:00', 19.110001): 0.0,
  ('2020-06-26 06:59:00', 19.119999): 0.0,
  ('2020-06-26 07:00:00', 19.060001): 5.0,
  ('2020-06-26 07:00:00', 19.07): 9.0,
  ('2020-06-26 07:00:00', 19.080002): 7.0},
 'pos': {('2020-06-26 06:59:00', 19.07): 0.0,
  ('2020-06-26 06:59:00', 19.080002): 1.0,
  ('2020-06-26 06:59:00', 19.09): 3.0,
  ('2020-06-26 06:59:00', 19.099998): 8.0,
  ('2020-06-26 06:59:00', 19.110001): 14.0,
  ('2020-06-26 06:59:00', 19.119999): 4.0,
  ('2020-06-26 07:00:00', 19.060001): 10.0,
  ('2020-06-26 07:00:00', 19.07): 4.0,
  ('2020-06-26 07:00:00', 19.080002): 7.0}}

And there's another dataframe that has the information of the relationship between time and its coordinates还有一个 dataframe 有时间和坐标关系的信息

>>> df_idx
                     x_cor
time                      
...
2020-06-26 06:55:00     25
2020-06-26 06:56:00     26
2020-06-26 06:57:00     27
2020-06-26 06:58:00     28
2020-06-26 06:59:00     29
2020-06-26 07:00:00     30
2020-06-26 07:01:00     31
2020-06-26 07:02:00     32
2020-06-26 07:03:00     33
2020-06-26 07:04:00     34
2020-06-26 07:05:00     35
2020-06-26 07:06:00     36
2020-06-26 07:07:00     37
2020-06-26 07:08:00     38
2020-06-26 07:09:00     39
2020-06-26 07:10:00     40
...

What I want to do is to use x_cor column as another index of df .我想要做的是使用x_cor列作为df的另一个索引。 The result would look like:结果将如下所示:

>>> df
                                          neg      pos
time                x_cor      level                      
2020-06-26 06:59:00   29   19.070000      4.0      0.0
                           19.080002      0.0      1.0
                           19.090000      9.0      3.0
                           19.099998      1.0      8.0
                           19.110001      0.0     14.0
                           19.119999      0.0      4.0
2020-06-26 07:00:00   30   19.060001      5.0     10.0
                           19.070000      9.0      4.0
                           19.080002      7.0      7.0
...

How can I do this?我怎样才能做到这一点?

Use MultiIndex.get_level_values to get the index values at level 0 , then use map to map these values with values from df_idx dataframes x_cor values, finally use set_index with append=True to append the new index l1 and use MultiIndex.swaplevel : Use MultiIndex.get_level_values to get the index values at level 0 , then use map to map these values with values from df_idx x_cor values, finally use set_index with append=True to append the new index l1 and use MultiIndex.swaplevel :

l1 = df.index.get_level_values(0).map(df_idx['x_cor']).rename('x_cor')
df1 = df.set_index(l1, append=True).swaplevel(1, 2)

Result:结果:

print(df1)
                                     neg   pos
time                x_cor level               
2020-06-26 06:59:00 29    19.070000  4.0   0.0
                          19.080002  0.0   1.0
                          19.090000  9.0   3.0
                          19.099998  1.0   8.0
                          19.110001  0.0  14.0
                          19.119999  0.0   4.0
2020-06-26 07:00:00 30    19.060001  5.0  10.0
                          19.070000  9.0   4.0
                          19.080002  7.0   7.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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