我们是新程序员,我们的问题似乎是 weight是一个对象,例如15'2,我们正试图将其变成一个数字,例如15.2,我们似乎无法弄清楚,我们尝试过的某些功能是
def value_to_int(df_value):
try:
value = float(df_value[1:-1])
suffix = df_value[-1:]
if suffix == 'M':
value = value * 1000000
elif suffix == 'K':
value = value * 1000
except ValueError:
value = 0
return value
df['Value_float'] = df['Value'].apply(value_to_int)
TypeError Traceback (most recent call last)
<ipython-input-143-9a5ed573b054> in <module>()
14 return value
15
---> 16 df['Value_float'] = df['Height'].apply(value_to_int)
~\Anaconda3\lib\site-packages\pandas\core\series.py in apply(self, func, convert_dtype, args, **kwds)
3192 else:
3193 values = self.astype(object).values
-> 3194 mapped = lib.map_infer(values, f, convert=convert_dtype)
3195
3196 if len(mapped) and isinstance(mapped[0], Series):
pandas/_libs/src\inference.pyx in pandas._libs.lib.map_infer()
<ipython-input-143-9a5ed573b054> in value_to_int(df_value)
3 def value_to_int(df_value):
4 try:
----> 5 value = float(df_value[1:-1])
6 suffix = df_value[-1:]
7
TypeError: 'float' object is not subscriptable
答案 0 :(得分:0)
如果要将“ 12'5”转换为浮点型,首先需要将“”替换为。因此您的代码将如下所示:
def value_to_int(df_value):
try:
value = float(df_value[1:-1].replace("'", "."))
suffix = df_value[-1:]
if suffix == 'M':
value = value * 1000000
elif suffix == 'K':
value = value * 1000
except ValueError:
value = 0
return value
df['Value_float'] = df['Value'].apply(value_to_int)
最好!
答案 1 :(得分:0)
import pandas as pd
import re
df = pd.DataFrame(data={'val':["M15'2","K13'3", "17'4"]})
df['sex'] = df['val'].apply(lambda x: " ".join(re.findall("[a-zA-Z]+", x)))
# split string on sex
def split_str(row):
if row['sex']:
return row['val'].split(row['sex']).pop()
else:
return row['val']
df['weight'] = df.apply(lambda row: split_str(row), axis=1)
df['weight'] = df['weight'].apply(lambda x: x.replace("'", "."))
df['weight'] = df['weight'].astype(float)
# modify weight on sex
def multiply_weight(row):
if row['sex'] == 'M':
row['weight'] = row['weight'] * 1000000
elif row['sex'] == 'K':
row['weight'] = row['weight'] * 1000
else:
row['weight'] = 0
return row
# return final df
df = df.apply(lambda row: multiply_weight(row), axis=1)