나는 느낌을 메이트,나는 같은 종류의 문제입니다. 하지만 다행히도 이것은 어렵지 않아
import pandas as pd
df = pd.DataFrame({'height': [16, 7, '6m', '2.4', '3,5', 'Asdf', '9;6;3']})
df['height'] = df['height'].astype(str) # force type str
df['height'] = df['height'].str.replace('.', ',', regex=False) # . -> ,
df['height'] = df['height'].str.replace('[A-Za-z]', '') # remove all characters (regex)
df['height'] = df['height'].str.split(';').apply(max) # pick largest value from 9;6;3
df['height'] = pd.to_numeric(df['height'], errors='coerce') # force float
당신
height
0 16.0
1 7.0
2 6.0
3 2.4
4 3.5
5 NaN
6 9.0
지금 당신을 변환하는 미터 피트(나는 가정하면 기본값은 미터)해야 합니다 추가 수준의 안색
import pandas as pd
import numpy as np
import re
def feet_to_m(s):
if '\'' in s or "\"" in s:
if '\'' in s:
feet = float(s.split('\'')[0])
else:
feet = 0
if '\"' in s:
if '\'' in s:
inch = float(s.split('\'')[1].split('\"')[0])
else:
inch = float(s.split('\"')[0])
else:
inch = 0
return (feet*12 + inch) * 0.0254
else:
return s
df = pd.DataFrame({'height': [16, 7, '6m', '2.4', '3,5', 'Asdf', '9;6;3', "11' 4\"", "4'", "15\""]})
df['height'] = df['height'].astype(str) # force type str
df['height'] = df['height'].str.replace(',', '.', regex=False) # . -> ,
df['height'] = df['height'].str.replace('[A-Za-z]', '') # remove all characters
df['height'] = df['height'].str.split(';').apply(max) # pick largest value from 9;6;3
df['height'] = df['height'].apply(feet_to_m)
df['height'] = pd.to_numeric(df['height'], errors='coerce') # force float
을 얻
height
0 16.0000
1 7.0000
2 6.0000
3 2.4000
4 3.5000
5 NaN
6 9.0000
7 3.4544
8 1.2192
9 0.3810
이