Reputation: 161
I would like to replace all value in a dataframe with a constant value, but just got an error. It would be really great if you could help me on that.
Below here is the dataframe and later on I have tried to replace the values in the dataframe with a constant value of 32.05.
import numpy as np
df = pd.DataFrame({'date': pd.date_range(start = '2020-01-01', end = '2020-12-31', freq = 'H')})
df['dtDate'] = df['date'].dt.date
df['intHour'] = df['date'].dt.time
df['dblPrice'] = 45 + 10*np.random.rand(len(df))
df = df.drop('date', axis = 1)
pivot = df.pivot(index = 'dtDate', columns = 'intHour', values = 'dblPrice')
And got this dataframe
intHour 00:00:00 01:00:00 02:00:00 03:00:00 04:00:00 05:00:00 06:00:00 07:00:00 08:00:00 09:00:00 ... 14:00:00 15:00:00 16:00:00 17:00:00 18:00:00 19:00:00 20:00:00 21:00:00 22:00:00 23:00:00
dtDate
2020-01-01 52.538754 51.266834 51.373766 45.931978 52.609328 50.994931 51.243347 54.008440 53.313223 50.066071 ... 47.631150 49.151051 51.230233 45.994514 46.603801 47.576478 54.277707 47.650023 45.535775 51.245288
2020-01-02 54.591999 54.810271 50.449986 46.800304 49.747234 50.002123 51.382932 53.773221 49.190824 54.865301 ... 48.188451 50.980094 52.985792 53.854782 48.715501 53.419668 45.958799 50.721659 50.025296 46.692690
2020-01-03 50.666929 50.127614 52.988890 49.797910 52.668042 46.745173 53.233271 52.660249 50.293708 54.212875 ... 52.933317 53.053633 49.817718 53.712133 48.702070 53.183621 51.698917 48.601872 45.762772 54.713565
2020-01-04 53.210317 54.504347 48.990980 46.313111 49.630296 54.855403 47.051514 53.416724 52.126807 46.779938 ... 54.823527 48.211544 45.756259 48.990539 54.049532 54.134094 49.649077 49.462418 47.850721 46.913194
2020-01-05 48.525838 51.466914 47.646876 47.655881 51.143606 50.072440 48.010113 51.275604 47.169781 45.386516 ... 49.845797 48.682241 51.143187 52.643786 50.871959 54.621818 47.472802 50.818774 50.479920 54.320502
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
2020-12-27 50.223507 50.919980 48.415068 48.236561 45.915901 49.851045 47.235178 45.852695 46.108208 46.500174 ... 53.986690 50.835382 54.937447 47.390783 45.074738 47.800815 53.589714 50.987971 49.597049 52.153157
2020-12-28 50.671287 54.534284 46.404504 47.427804 49.860974 54.858333 53.127082 46.716249 49.664347 52.813928 ... 51.245018 49.970843 54.989815 47.029045 52.105651 48.829855 53.768145 45.260424 48.351198 48.826515
2020-12-29 47.865982 51.279802 45.939696 50.316662 46.864319 50.615450 51.361867 49.743674 49.088331 48.515341 ... 50.221210 47.246730 51.632251 47.740422 47.947939 45.895385 46.152617 52.852860 52.449546 51.313461
2020-12-30 51.101144 50.516915 49.122323 46.505722 53.351985 52.348676 54.501081 46.962222 54.579690 46.476254 ... 50.938915 47.224380 50.406062 54.143802 46.059016 48.459780 48.465229 46.146354 50.951462 48.931579
2020-12-31 49.292937 NaN NaN NaN NaN NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
Then I would like to replace all value in the dataframe with a constant value. using
def replace(x):
z = 32.05
x = z
return (x)
pivot[1:] = pivot[1:].apply(replace)
pivot[:1] = pivot[:1].apply(replace)
pivot
but getting this error
ValueError: cannot set using a slice indexer with a different length than the value
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
/var/folders/3d/5tmbtv_x705103j1f7_ct0dw0000gn/T/ipykernel_29504/2503501402.py in <module>
4 return (x)
5
----> 6 pivot[1:] = pivot[1:].apply(replace)
7 pivot[:1] = Electricity_price_pivot[:1].apply(replace)
8 pivot
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/frame.py in __setitem__(self, key, value)
3153 # either we have a slice or we have a string that can be converted
3154 # to a slice for partial-string date indexing
-> 3155 return self._setitem_slice(indexer, value)
3156
3157 if isinstance(key, DataFrame) or getattr(key, "ndim", None) == 2:
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/frame.py in _setitem_slice(self, key, value)
3168 # backwards-compat, xref GH#31469
3169 self._check_setitem_copy()
-> 3170 self.iloc[key] = value
3171
3172 def _setitem_array(self, key, value):
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/indexing.py in __setitem__(self, key, value)
690
691 iloc = self if self.name == "iloc" else self.obj.iloc
--> 692 iloc._setitem_with_indexer(indexer, value, self.name)
693
694 def _validate_key(self, key, axis: int):
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/indexing.py in _setitem_with_indexer(self, indexer, value, name)
1635 self._setitem_with_indexer_split_path(indexer, value, name)
1636 else:
-> 1637 self._setitem_single_block(indexer, value, name)
1638
1639 def _setitem_with_indexer_split_path(self, indexer, value, name: str):
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/indexing.py in _setitem_single_block(self, indexer, value, name)
1859 # actually do the set
1860 self.obj._consolidate_inplace()
-> 1861 self.obj._mgr = self.obj._mgr.setitem(indexer=indexer, value=value)
1862 self.obj._maybe_update_cacher(clear=True)
1863
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/internals/managers.py in setitem(self, indexer, value)
566
567 def setitem(self, indexer, value) -> "BlockManager":
--> 568 return self.apply("setitem", indexer=indexer, value=value)
569
570 def putmask(self, mask, new, align: bool = True, axis: int = 0):
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/internals/managers.py in apply(self, f, align_keys, ignore_failures, **kwargs)
425 applied = b.apply(f, **kwargs)
426 else:
--> 427 applied = getattr(b, f)(**kwargs)
428 except (TypeError, NotImplementedError):
429 if not ignore_failures:
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/internals/blocks.py in setitem(self, indexer, value)
1006
1007 # length checking
-> 1008 check_setitem_lengths(indexer, value, values)
1009 exact_match = (
1010 len(arr_value.shape)
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/indexers.py in check_setitem_lengths(indexer, value, values)
169 if is_list_like(value):
170 if len(value) != length_of_indexer(indexer, values):
--> 171 raise ValueError(
172 "cannot set using a slice indexer with a "
173 "different length than the value"
ValueError: cannot set using a slice indexer with a different length than the value
Upvotes: 0
Views: 1927
Reputation: 55
If you want to replace the entire DataFrame with a constant value, simply use:
df.loc[:] = "your-value"
In your case:
pivot.loc[:] = 32.05
Upvotes: 1