I am trying to turn a series of heights that are in inches and turn them into cm amounts. below is the method I am using but am running into an issue that is also posted below. I have tried using regex but that did not work for me.
Calling the data head of a series
fighter_details.Height.head()
What the data looks like:
INDEX Data
0 NaN
1 5' 11"
2 6' 3"
3 5' 11"
4 5' 6"
Method I created to convert to cm
def inch_to_cm(x):
if x is np.NaN:
return x
else:
# format: '7\' 11"'
ht_ = x.split("' ")
ft_ = float(ht_[0])
in_ = float(ht_[1].replace("\"",""))
return ((12*ft_) + in_) * 2.54
Execution of method
fighter_details['Height'] = fighter_details['Height'].apply(inch_to_cm)
Error
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Input In [240], in <cell line: 1>()
----> 1 fighter_details['Height'] = fighter_details['Height'].apply(inch_to_cm)
File ~/opt/anaconda3/envs/book_env/lib/python3.8/site-packages/pandas/core/series.py:4108, in Series.apply(self, func, convert_dtype, args, **kwds)
4106 else:
4107 values = self.astype(object)._values
-> 4108 mapped = lib.map_infer(values, f, convert=convert_dtype)
4110 if len(mapped) and isinstance(mapped[0], Series):
4111 # GH 25959 use pd.array instead of tolist
4112 # so extension arrays can be used
4113 return self._constructor_expanddim(pd_array(mapped), index=self.index)
File pandas/_libs/lib.pyx:2467, in pandas._libs.lib.map_infer()
Input In [239], in inch_to_cm(x)
3 return x
4 else:
5 # format: '7\' 11"'
----> 6 ht_ = x.split("' ")
7 ft_ = float(ht_[0])
8 in_ = float(ht_[1].replace("\"",""))
AttributeError: 'float' object has no attribute 'split'