Home > Net >  Python: How can one use the map function with a dictionary?
Python: How can one use the map function with a dictionary?

Time:02-03

I'm trying to use the python map function with a dictionary but I'm getting a TypeError: string indices must be integers. I know there are other ways of accomplishing the same task but I'm trying to learn how to use map via a simple relevant example. How I can modify this to get it to work?

df = pd.DataFrame([[100,90,80,70,36,45], [101,78,65,88,55,78], [92,77,42,79,43,32], [103,98,76,54,45,65]], index = pd.date_range(start='2022-01-01' ,periods=4))

df.columns = pd.MultiIndex.from_tuples((("mkf", "Open"), ("mkf", "Close"), ("tdf", "Open"), ("tdf","Close"), ("ghi","Open"), ("ghi", "Close")))

dct = {k:df[k] for k in df.columns.levels[0]}

{'ghi':             Open  Close
 2022-01-01    36     45
 2022-01-02    55     78
 2022-01-03    43     32
 2022-01-04    45     65,
 'mkf':             Open  Close
 2022-01-01   100     90
 2022-01-02   101     78
 2022-01-03    92     77
 2022-01-04   103     98,
 'tdf':             Open  Close
 2022-01-01    80     70
 2022-01-02    65     88
 2022-01-03    42     79
 2022-01-04    76     54}

def test(df):
    df.loc[:,'ind'] = (abs(df['Close'] / df['Open'] - 1) < 0.0025)
    return df

list(map(test, dct))

TypeError                                 Traceback (most recent call last)
<ipython-input-55-aa370b545171> in <module>
----> 1 list(map(test, dict))

<ipython-input-39-339a20e273ad> in test(df)
      1 def test(df):
----> 2     df.loc[:,'ind'] = (abs(df['Close'] / df['Open'] - 1) < 0.0025)
      3     return df
      4

TypeError: string indices must be integers

CodePudding user response:

You can zip the mapped dct values with dct.keys and wrap it with a dict constructor. You probably want to make a copy of the passed DataFrame to test to avoid SettingWithCopyWarning too.

def test(df):
    df = df.copy()
    ...

out = dict(zip(dct.keys(), map(test, dct.values())))

Output:

{'ghi':             Open  Close    ind
 2022-01-01    36     45  False
 2022-01-02    55     78  False
 2022-01-03    43     32  False
 2022-01-04    45     65  False,
 'mkf':             Open  Close    ind
 2022-01-01   100     90  False
 2022-01-02   101     78  False
 2022-01-03    92     77  False
 2022-01-04   103     98  False,
 'tdf':             Open  Close    ind
 2022-01-01    80     70  False
 2022-01-02    65     88  False
 2022-01-03    42     79  False
 2022-01-04    76     54  False}

CodePudding user response:

You should pass dict.values()

list(map(test, dct.values()))
  •  Tags:  
  • Related