Alok
Alok

Reputation: 23

Convert feet to CM

    Name                  Team      Number  Position    Age    Height   Weight  College           Salary
1   Jae Crowder     Boston Celtics  99.0    SF          25.0    6-6      235.0  Marquette       6796117.0
2   John Holland    Boston Celtics  30.0    SG  2       7.0      6-5     205.0  Boston University   NaN

Hi, I have this table and I want to convert the Height from feet to CM in new column, the height column RN isn't float neither int so I need to extract it first and make a manipulate on the number.

Thenks

Upvotes: 2

Views: 2108

Answers (3)

Akshay Sehgal
Akshay Sehgal

Reputation: 19307

Using str functions and dot

A potentially faster way (vectorized) would be using str functions.

The operation you want to perform can be done by applying a dot-product between the 2 elements in your height column and the 2 conversion multipliers.

[feet, inches] @ [30.48, 2.54] = feet*30.48 + inches*2.54
  1. The str.split breaks the string into a list.
  2. Then apply(pd.Series) breaks the list into 2 separate columns.
  3. Finally, astype(int) converts each of the cells to int
  4. Last, dot performs a dot product on each row with the conversion multipliers.
conversions = [30.48, 2.54]
df['new'] = df['Height'].str.split('-').apply(pd.Series).astype(int).dot(conversions)
0    198.12
1    167.64
2     35.56
dtype: float64

Using lambda functions and dot

If you are more comfortable with lambda functions, here is the code for the same -

conversions = [30.48, 2.54]

df['new'] = df['Height'].apply(lambda x: pd.Series(map(int, x.split('-'))).dot(conversions))
0    198.12
1    167.64
2     35.56
dtype: float64

Upvotes: 1

Rafa
Rafa

Reputation: 684

Assuming that the only values in the column would be strings with 3 characters (number_of_feet, '-', number_of_inches), then the solution could be something like this:

your_table['Height'].apply(lambda x: int(x.split('-')[0])*30.48 + int(x.split('-')[1])*2.54)

However, as I can see from second row of your table, you have some additional characters in your strings, which might require additional preprocessing (in this case spaces, but they won't impact the code - the string should easily be changed to integer).

Upvotes: 0

houseofleft
houseofleft

Reputation: 447

Looks like a good step would be to split your height column into two (feet + inches) first, and then go from there:

df['feet'] = df['Height'].apply(lambda x: int(x.split('-')[0]))
df['inches'] = df['Height'].apply(lambda x: int(x.split('-')[1]))
df['feet_in_cm'] = df['feet'] * 30.48
df['inches_in_cm'] = df['inches'] * 2.54
df['height_in_cm'] = df['feet_in_cm'] + df['inches_in_cm']

Upvotes: 0

Related Questions