[英]Trying to replace age by mean in pandas, how do i do it?
df.head()
給出:
Record ID Agency Code Agency Name Agency Type City State Year Month Incident Crime Type ... Victim Ethnicity Perpetrator Sex Perpetrator Age Perpetrator Race Perpetrator Ethnicity Relationship Weapon Victim Count Perpetrator Count Record Source
0 1 AK00101 Anchorage Municipal Police Anchorage Alaska 1980 January 1 Murder or Manslaughter ... Unknown Male 15 Native American/Alaska Native Unknown Acquaintance Blunt Object 0 0 FBI
1 2 AK00101 Anchorage Municipal Police Anchorage Alaska 1980 March 1 Murder or Manslaughter ... Unknown Male 42 White Unknown Acquaintance Strangulation 0 0 FBI
2 3 AK00101 Anchorage Municipal Police Anchorage Alaska 1980 March 2 Murder or Manslaughter ... Unknown Unknown 0 Unknown Unknown Unknown Unknown 0 0 FBI
3 4 AK00101 Anchorage Municipal Police Anchorage Alaska 1980 April 1 Murder or Manslaughter ... Unknown Male 42 White Unknown Acquaintance Strangulation 0 0 FBI
4 5 AK00101 Anchorage Municipal Police Anchorage Alaska 1980 April 2 Murder or Manslaughter ... Unknown Unknown 0 Unknown Unknown Unknown Unknown 0 1 FBI
df.loc(df['Perpetrator Age']< 5)['Perpetrator Age'] = df['Perpetrator age'].mean()
給出:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-38-8799b2e024ea> in <module>()
----> 1 df.loc(df['Perpetrator Age']< 5)['Perpetrator Age'] = df['Perpetrator Age'].mean()
c:\python27\lib\site-packages\pandas\core\indexing.pyc in __call__(self, axis)
100
101 if axis is not None:
--> 102 axis = self.obj._get_axis_number(axis)
103 new_self.axis = axis
104 return new_self
c:\python27\lib\site-packages\pandas\core\generic.pyc in _get_axis_number(cls, axis)
349 @classmethod
350 def _get_axis_number(cls, axis):
--> 351 axis = cls._AXIS_ALIASES.get(axis, axis)
352 if is_integer(axis):
353 if axis in cls._AXIS_NAMES:
c:\python27\lib\site-packages\pandas\core\generic.pyc in __hash__(self)
1814 def __hash__(self):
1815 raise TypeError('{0!r} objects are mutable, thus they cannot be'
-> 1816 ' hashed'.format(self.__class__.__name__))
1817
1818 def __iter__(self):
TypeError: 'Series' objects are mutable, thus they cannot be hashed
嘗試
df['Perpetrator Age'][df['Perpetrator Age'] < 5] = df['Perpetrator Age'].mean()
使用DataFrame.loc
來防止SettingWithCopyWarning
與掩碼和列進行替換:
df.loc[df['Perpetrator Age'] < 5, 'Perpetrator Age'] = df['Perpetrator Age'].mean()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.