0

Based on the list comprehension solution of Python dictionary: Get list of values for list of keys question, I'm trying to do the same for my dict "values-extraction" but I get either 'Series' objects are mutable, thus they cannot be hashed error or unhashable type: 'list' :( please help.

I have a dict :

region_colors = {
 'Speyside': '#0173b2',
 'Highlands': '#de8f05',
 'Lowlands': '#029e73',
 'Islands': '#d55e00',
 'Campbelltown': '#cc78bc',
 'Islay': '#ca9161'}

and a list:

region_colsbis = [[
  'Highlands',
  'Speyside',
  'Speyside',
  'Speyside',
  'Speyside',
  'Lowlands',
  'Speyside',
  'Speyside',
  'Islay',
  'Highlands',
  ...,
  'Highlands']]

which actually comes from an extraction of a dataframe column.

I need to convert this past list into the value elements of the dict region_colors, so that every time the region_colsbis list says 'Highlands' a new list (say region_cols) reflects '#de8f05'

expected output:

region_cols = ['#de8f05', '#0173b2', '#0173b2', '#0173b2', '#0173b2', '#029e73', ...]

I've tried doing

[region_colors[x] for x in region_colsbis]

and

for element in region_colsbis:
    region_cols.append(region_colors[element].value)

but I keep getting the errors mentioned before. Could you demigods of Python please help this humble peasant?

wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • 2
    The problem is that your list of `region_colsbis` is nested because you have double-[ there. Just use a single [ and it should be fine. – cadolphs Oct 20 '21 at 19:55
  • This is totally off-topic, but I have a grammar note: A semicolon (`;`) is used to join two *independent* clauses together, but a clause starting with "but" or "so that" is *dependent*. I fixed them for you. – wjandrea Oct 20 '21 at 20:01
  • How are you extracting `region_colsbis` from the df column? That seems to be where the problem is. BTW, you might want to read about the [XY problem](https://meta.stackexchange.com/q/66377/343832). – wjandrea Oct 20 '21 at 20:06
  • BTW, welcome to Stack Overflow! Check out the [tour], and [ask] if you want tips. – wjandrea Oct 20 '21 at 20:06
  • Guys, I didnt know about the issues a nested list could bring and I actually hadnt notice it, thanks for all the help, the solution you proposed works perfectly and I will be more careful next time I work with DF. Awesome welcoming for me to Stackoverflow world, thanks! Ill see you next time – Héctor Ocampo Ruiz Oct 20 '21 at 22:24

1 Answers1

0

region_colsbis is a nested list. So in this case, you can select the first sublist, region_colsbis[0].

[region_colors[x] for x in region_colsbis[0]]

However, this may fail if it's not guaranteed that len(region_colsbis) == 1, and you haven't said how you're extracting it from the dataframe column, so I don't know.

There might actually be a better way to do it, like Series.map(region_colors):

df = pd.DataFrame({
    'regions': [
        'Highlands',
        'Speyside',
        'Speyside',
        'Speyside',
        'Speyside',
        'Lowlands',
        'Speyside',
        'Speyside',
        'Islay',
        'Highlands',
        'Highlands']
    })

df['regions'].map(region_colors).rename('region_cols')

Output:

0     #de8f05
1     #0173b2
2     #0173b2
3     #0173b2
4     #0173b2
5     #029e73
6     #0173b2
7     #0173b2
8     #ca9161
9     #de8f05
10    #de8f05
Name: region_cols, dtype: object
wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • So I extracted the list from the DF using this: region_colsbis=[(whisky['Region']).astype(str).values.tolist()] as otherwise I would´ve ended with a Series like object, something like [0, 'Highlands'\n, ...] and probably thats why I ended with a nested list, the len of region_colsbis is indeed ==1 as you just predicted and the solution you gave works perfect, Thank you very much! and thanks for the grammar tip and the welcoming too! Have an amazing day! – Héctor Ocampo Ruiz Oct 20 '21 at 22:20
  • @Héctor Oh, I see, you added brackets on the outside. Here's how to fix that: `region_colsbis = whisky['Region'].astype(str).values.tolist()`. I also removed the extra parens. Welcome! Don't forget to [upvote answers you find useful and accept the best one](/help/someone-answers) :) – wjandrea Oct 20 '21 at 22:24