I have a simple pandas
Dataframe like this:
<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:
<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:
<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:
<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...