gpt4 book ai didi

python - 仅缩放数据框中包含字符串的数值

转载 作者:行者123 更新时间:2023-12-01 06:46:04 26 4
gpt4 key购买 nike

我在 python 中,我尝试缩放数据框

subject_id hour_measure         urinecolor   blood pressure                  
3 1.00 red 40
1.15 red high
4 2.00 yellow low

因为它包含数字和文本列下面的代码给了我错误

 #MinMaxScaler for Data
scaler = MinMaxScaler(copy=True, feature_range=(0, 1))
X = scaler.fit_transform(X)

它给了我错误,因为数据帧包含字符串,我如何告诉Python只缩放包含数字的列,并缩放字符串列中的数值。

最佳答案

将非数值转换为缺失值,然后使用 alternative solution为了缩放,最后将缺失值替换回原始值:

print (df)
subject_id hour_measure urinecolor blood pressure
0 3 1.00 red 40
1 3 1.15 red high
2 4 2.00 yellow low
3 5 5.00 yellow 100

df = df.set_index('subject_id')

df1 = df.apply(lambda x: pd.to_numeric(x, errors='coerce'))
df2 = (df1 - df1.min()) / (df1.max() - df1.min())

df = df2.combine_first(df)
print (df)
hour_measure urinecolor blood pressure
subject_id
3 0.0000 red 0
3 0.0375 red high
4 0.2500 yellow low
5 1.0000 yellow 1
<小时/>

第一个解决方案:

我建议通过字典将文本列替换为数字,例如:

dbp = {'high': 150, 'low': 60}

df['blood pressure'] = df['blood pressure'].replace(dbp)

一起:

#if subject_id are numeric convert them to index
df = df.set_index('subject_id')

dbp = {'high': 150, 'low': 60}
#replace to numbers and convert to integers
df['blood pressure'] = df['blood pressure'].replace(dbp).astype(int)

print (df)
hour_measure urinecolor blood pressure
subject_id
3 1.00 red 40
3 1.15 red 150
4 2.00 yellow 60

print (df.dtypes)
hour_measure float64
urinecolor object
blood pressure int32
dtype: object
<小时/>
from sklearn import preprocessing

scaler = preprocessing.MinMaxScaler(copy=True, feature_range=(0, 1))
#select only numeric columns
X = scaler.fit_transform(df.select_dtypes(np.number))
print (X)
[[0. 0. ]
[0.15 1. ]
[1. 0.18181818]]

详细信息:

print (df.select_dtypes(np.number))
hour_measure blood pressure
subject_id
3 1.00 40
3 1.15 150
4 2.00 60

关于python - 仅缩放数据框中包含字符串的数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59224416/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com