[英]converting feet-inch string DataFrame column to centimeters
我有一列籃球運動員的身高:
0 6-10
1 6-9
2 7-2
3 6-1
4 6-6
...
4545 6-11
4546 7-1
4547 6-1
4548 7-1
4549 6-3
我想將值從英尺轉換為厘米。 我進行了拆分: player_data['height'].str.split('-')
,並收到了一系列 arrays 的獨立英尺和英寸:
0 [6, 10]
1 [6, 9]
2 [7, 2]
3 [6, 1]
4 [6, 6]
...
4545 [6, 11]
4546 [7, 1]
4547 [6, 1]
4548 [7, 1]
4549 [6, 3]
現在我嘗試將值轉換為浮點數:
df = player_data['height'].str.split('-').astype(float)
但是我收到一個錯誤: ValueError: setting an array element with a sequence.
我做錯了什么?
假設您的最終目標是將“英尺-英寸”值轉換為厘米,我會這樣做:
df['height_cm'] = (df['height'].str.extract('(\d*)-(\d*)')
.apply(pd.to_numeric, errors='coerce') # or .astype(int)
.mul([30.48, 2.54]).sum(axis=1)
)
Output:
height height_cm
0 6-10 208.28
1 6-9 205.74
2 7-2 218.44
3 6-1 185.42
4 6-6 198.12
4545 6-11 210.82
4546 7-1 215.90
4547 6-1 185.42
4548 7-1 215.90
4549 6-3 190.50
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.