How to correctly replace an element in a pd.Dataframe with a list?

I have a simple pandas Dataframe like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>test_df = pd.DataFrame(
{
0: [
"Setup",
"Dissection",
"Specimen Removal",
"Closure",
]
}
)
</code>
<code>test_df = pd.DataFrame( { 0: [ "Setup", "Dissection", "Specimen Removal", "Closure", ] } ) </code>
test_df = pd.DataFrame(
    {
        0: [
            "Setup",
            "Dissection",
            "Specimen Removal",
            "Closure",
        ]
    }
)

I want to replace the values in column 0 with colour values, like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>colours = {
"Setup": [0.12156862745098039, 0.4666666666666667, 0.7058823529411765, 1.0],
"Dissection": [0.8392156862745098, 0.15294117647058825, 0.1568627450980392, 1.0],
"Specimen Removal": [
0.9686274509803922,
0.7137254901960784,
0.8235294117647058,
1.0,
],
"Closure": [0.6196078431372549, 0.8549019607843137, 0.8980392156862745, 1.0],
}
test_df.replace(colours)
</code>
<code>colours = { "Setup": [0.12156862745098039, 0.4666666666666667, 0.7058823529411765, 1.0], "Dissection": [0.8392156862745098, 0.15294117647058825, 0.1568627450980392, 1.0], "Specimen Removal": [ 0.9686274509803922, 0.7137254901960784, 0.8235294117647058, 1.0, ], "Closure": [0.6196078431372549, 0.8549019607843137, 0.8980392156862745, 1.0], } test_df.replace(colours) </code>
colours = {
    "Setup": [0.12156862745098039, 0.4666666666666667, 0.7058823529411765, 1.0],
    "Dissection": [0.8392156862745098, 0.15294117647058825, 0.1568627450980392, 1.0],
    "Specimen Removal": [
        0.9686274509803922,
        0.7137254901960784,
        0.8235294117647058,
        1.0,
    ],
    "Closure": [0.6196078431372549, 0.8549019607843137, 0.8980392156862745, 1.0],
}

test_df.replace(colours)

But running this gives me an error:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>File ~/.venv/fastai/lib/python3.11/site-packages/pandas/core/internals/blocks.py:729, in Block.replace(self, to_replace, value, inplace, mask, using_cow)
725 elif self._can_hold_element(value):
726 # TODO(CoW): Maybe split here as well into columns where mask has True
727 # and rest?
728 blk = self._maybe_copy(using_cow, inplace)
--> 729 putmask_inplace(blk.values, mask, value)
730 if not (self.is_object and value is None):
731 # if the user *explicitly* gave None, we keep None, otherwise
732 # may downcast to NaN
733 blocks = blk.convert(copy=False, using_cow=using_cow)
File ~/.venv/fastai/lib/python3.11/site-packages/pandas/core/array_algos/putmask.py:56, in putmask_inplace(values, mask, value)
54 values[mask] = value[mask]
55 else:
---> 56 values[mask] = value
57 else:
58 # GH#37833 np.putmask is more performant than __setitem__
59 np.putmask(values, mask, value)
ValueError: NumPy boolean array indexing assignment cannot assign 4 input values to the 1 output values where the mask is true
</code>
<code>File ~/.venv/fastai/lib/python3.11/site-packages/pandas/core/internals/blocks.py:729, in Block.replace(self, to_replace, value, inplace, mask, using_cow) 725 elif self._can_hold_element(value): 726 # TODO(CoW): Maybe split here as well into columns where mask has True 727 # and rest? 728 blk = self._maybe_copy(using_cow, inplace) --> 729 putmask_inplace(blk.values, mask, value) 730 if not (self.is_object and value is None): 731 # if the user *explicitly* gave None, we keep None, otherwise 732 # may downcast to NaN 733 blocks = blk.convert(copy=False, using_cow=using_cow) File ~/.venv/fastai/lib/python3.11/site-packages/pandas/core/array_algos/putmask.py:56, in putmask_inplace(values, mask, value) 54 values[mask] = value[mask] 55 else: ---> 56 values[mask] = value 57 else: 58 # GH#37833 np.putmask is more performant than __setitem__ 59 np.putmask(values, mask, value) ValueError: NumPy boolean array indexing assignment cannot assign 4 input values to the 1 output values where the mask is true </code>
File ~/.venv/fastai/lib/python3.11/site-packages/pandas/core/internals/blocks.py:729, in Block.replace(self, to_replace, value, inplace, mask, using_cow)
    725 elif self._can_hold_element(value):
    726     # TODO(CoW): Maybe split here as well into columns where mask has True
    727     # and rest?
    728     blk = self._maybe_copy(using_cow, inplace)
--> 729     putmask_inplace(blk.values, mask, value)
    730     if not (self.is_object and value is None):
    731         # if the user *explicitly* gave None, we keep None, otherwise
    732         #  may downcast to NaN
    733         blocks = blk.convert(copy=False, using_cow=using_cow)

File ~/.venv/fastai/lib/python3.11/site-packages/pandas/core/array_algos/putmask.py:56, in putmask_inplace(values, mask, value)
     54         values[mask] = value[mask]
     55     else:
---> 56         values[mask] = value
     57 else:
     58     # GH#37833 np.putmask is more performant than __setitem__
     59     np.putmask(values, mask, value)

ValueError: NumPy boolean array indexing assignment cannot assign 4 input values to the 1 output values where the mask is true

What am I doing wrong?

IIUC use:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>test_df[0] = test_df[0].map(colours)
print (test_df)
0
0 [0.12156862745098039, 0.4666666666666667, 0.70...
1 [0.8392156862745098, 0.15294117647058825, 0.15...
2 [0.9686274509803922, 0.7137254901960784, 0.823...
3 [0.6196078431372549, 0.8549019607843137, 0.898...
</code>
<code>test_df[0] = test_df[0].map(colours) print (test_df) 0 0 [0.12156862745098039, 0.4666666666666667, 0.70... 1 [0.8392156862745098, 0.15294117647058825, 0.15... 2 [0.9686274509803922, 0.7137254901960784, 0.823... 3 [0.6196078431372549, 0.8549019607843137, 0.898... </code>
test_df[0] = test_df[0].map(colours)

print (test_df)
                                                   0
0  [0.12156862745098039, 0.4666666666666667, 0.70...
1  [0.8392156862745098, 0.15294117647058825, 0.15...
2  [0.9686274509803922, 0.7137254901960784, 0.823...
3  [0.6196078431372549, 0.8549019607843137, 0.898...

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật