簡體   English   中英

在系列上將英寸轉換為 CM

[英]Converting inches to CM on series

我正在嘗試將一系列以英寸為單位的高度轉換為厘米數。 以下是我正在使用的方法,但遇到了一個問題,該問題也在下面發布。 我嘗試過使用正則表達式,但這對我不起作用。

調用一個系列的數據頭

fighter_details.Height.head()

數據是什么樣子的:

INDEX   Data
0       NaN
1       5' 11"
2       6' 3"
3       5' 11"
4       5' 6"

我創建的轉換為 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

方法的執行

fighter_details['Height'] = fighter_details['Height'].apply(inch_to_cm)

錯誤

---------------------------------------------------------------------------
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'

看起來您使用了錯誤的列。

也就是說,為了提高效率,最好使用矢量方法。

您可以提取 ft/in 分量,將每個分量轉換為 cm 並求和:

df['Data_cm'] = (df['Data']
 .str.extract(r'(\d+)\'\s*(\d+)"')
 .astype(float)
 .mul([12*2.54, 2.54])
 .sum(axis=1)
 )

輸出:

   INDEX    Data  Data_cm
0      0     NaN     0.00
1      1  5' 11"   180.34
2      2   6' 3"   190.50
3      3  5' 11"   180.34
4      4   5' 6"   167.64

問題似乎是您使用了錯誤的列名'Height'但它必須是'Data'


最小的工作代碼:

import pandas as pd
import numpy as np

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

fighter_details = pd.DataFrame({
    "Data": [np.NaN, '5\' 11"', '6\' 3"', '5\' 11"', '5\' 6"']
})    

print('\n--- before ---\n')
print(fighter_details)

fighter_details['Data'] = fighter_details['Data'].apply(inch_to_cm)

print('\n--- after ---\n')
print(fighter_details)

結果:

--- before ---

     Data
0     NaN
1  5' 11"
2   6' 3"
3  5' 11"
4   5' 6"

--- after ---

     Data
0     NaN
1  180.34
2  190.50
3  180.34
4  167.64

希望這會有所幫助...

def inch_to_cm(x):
  if x is np.NaN:
    return x
  else:
    ft,inc = x.split("'")
    inches = inc[1:-1]
    return ((12*int(ft)) + int(inches)) * 2.54

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM