[P/M/K] 2 way to transform from different types: .loc or mapping

2 way to transform from different types: .loc or mapping

When we have to tranform a column from type to type,here are two ways:
1

all_data.loc[all_data["edjefe"]=="yes","edjefe"]=1

2

mapping = {"yes": 1, "no": 0}
all_data['edjefe'] = all_data['edjefe'].replace(mapping).astype(np.float64)
# put it another way
all_data['edjefe'].replace({"yes": 1, "no": 0}).astype(np.float64)

猜你喜欢

转载自blog.csdn.net/qq_40820196/article/details/82416379