1

I print the report on terminal using below python code

print(kf.groupby(level='ErrorCode').apply(lambda x: x/x.sum()))

to get

ErrorCode  ResponseType        
0          CANCEL_ORDER_CONFIRM    0.7207
           TRADE_CONFIRM           0.2792
1          CANCEL_ORDER_CONFIRM    0.7207
           TRADE_CONFIRM           0.2792
dtype: float64

But the ErrorCode 0,1 have to be mapped to a different number. That mapping can be written in a file in any format. Currently I can write mapping file as

0 10.0.0.0
1 10.0.0.1
2 10.2.0.1

Format of above can be changed. Is there a way to replace grouped value output by mapped values. Final output would be like

ErrorCode           ResponseType        
0  10.0.0.0         CANCEL_ORDER_CONFIRM    0.7207
                    TRADE_CONFIRM           0.2792
1  10.0.0.1         CANCEL_ORDER_CONFIRM    0.7207
                    TRADE_CONFIRM           0.2792
dtype: float64

It is even ok, if this can be done in bash, but alignment should be readable.

If this is very tricky, then I would simply make ErrorCode column values to change according to mapping file (but I think that too is not a very good approach)

Update1: Similarly I have data.groupby([data['ErrorCode'], pd.Grouper(freq='1500T')])['latency'].describe().unstack(0)

and my output is

ErrorCode                      0               1
Time_req                                        
2017-03-08 count      111.000000      111.000000
           mean    251509.738739   357710.729730
           std     250469.755466   795885.356352
           min     104877.000000   111343.000000
           25%     132616.000000   131953.000000
           50%     160899.000000   163100.000000
           75%     261440.000000   279448.000000
           max    2071299.000000  8039122.000000

But I need to map ErrorCode using a dictionary named myd which is myd={0: '0 172.19.13.51', 1: '1 172.19.13.51'} such that output is something like below

ErrorCode         0 172.19.13.51  1 172.19.13.51
Time_req                                        
2017-03-08 count      111.000000      111.000000
           mean    251509.738739   357710.729730
           std     250469.755466   795885.356352
           min     104877.000000   111343.000000
           25%     132616.000000   131953.000000
           50%     160899.000000   163100.000000
           75%     261440.000000   279448.000000
           max    2071299.000000  8039122.000000
pythonRcpp
  • 2,042
  • 6
  • 26
  • 48

1 Answers1

1

You can use rename for replace all values from keys of dict to values:

d = {0: '10.0.0.0', 1: '10.0.0.1',2: '10.2.0.1'}
df = df.rename(index=d)

print (df)
ErrorCode  ResponseType        
10.0.0.0   CANCEL_ORDER_CONFIRM    0.7207
           TRADE_CONFIRM           0.2792
10.0.0.1   CANCEL_ORDER_CONFIRM    0.7207
           TRADE_CONFIRM           0.2792
Name: val, dtype: float64

If need keys and values together:

d = {0: '10.0.0.0', 1: '10.0.0.1',2: '10.2.0.1'}
d = {k:str(k) + ' ' + v for k, v in d.items()}
df = df.rename(index=d)

print (df)
ErrorCode   ResponseType        
0 10.0.0.0  CANCEL_ORDER_CONFIRM    0.7207
            TRADE_CONFIRM           0.2792
1 10.0.0.1  CANCEL_ORDER_CONFIRM    0.7207
            TRADE_CONFIRM           0.2792
Name: val, dtype: float64
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • how to understand / read `d = {k:str(k) + ' ' + v for k, v in d.items()}` I know it changes the key to ( key value concat ) – pythonRcpp Mar 17 '17 at 13:13
  • it loop each value of dist and concanecate key and value of each item of dict and get it to value and output is another dict. – jezrael Mar 17 '17 at 13:14
  • maybe [this](http://stackoverflow.com/a/14507637/2901002) explain it better, maybe also [this](https://www.python.org/dev/peps/pep-0274/) and [this](http://stackoverflow.com/documentation/python/196/comprehensions/738/dictionary-comprehensions#t=201703171317054989916) – jezrael Mar 17 '17 at 13:17
  • II think only need change `index` to `columns` - `df = df.rename(columns=d)` – jezrael Mar 22 '17 at 08:47