作为一个例子,我有一个mushroom数据集,其中包含数十个分类特征。我想把它装进去熊猫.DataFrame并转换为数字。样本的特征存储在列中,行表示不同的样本。因此,应将转换为数值应用于列。在R中,我只需要两行代码:
#Load the data. The features are categorical.
mushrooms <- read.csv("https://archive.ics.uci.edu/ml/machine-learning-databases/mushroom/agaricus-lepiota.data", header = FALSE, stringsAsFactors = TRUE)
#Convert the features to numeric. The features are stored in columns.
mushroomsNumeric <- data.frame(lapply(mushrooms, as.numeric))
# View the first 5 samples of the original data.
mushrooms[1:5,]
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 V17 V18 V19 V20 V21 V22 V23
1 p x s n t p f c n k e e s s w w p w o p k s u
2 e x s y t a f c b k e c s s w w p w o p n n g
3 e b s w t l f c b n e c s s w w p w o p n n m
4 p x y w t p f c n n e e s s w w p w o p k s u
5 e x s g f n f w b k t e s s w w p w o e n a g
# View the first 5 samples of the converted data.
mushroomsNumeric[1:5,]
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 V17 V18 V19 V20 V21 V22 V23
1 2 6 3 5 2 7 2 1 2 5 1 4 3 3 8 8 1 3 2 5 3 4 6
2 1 6 3 10 2 1 2 1 1 5 1 3 3 3 8 8 1 3 2 5 4 3 2
3 1 1 3 9 2 4 2 1 1 6 1 3 3 3 8 8 1 3 2 5 4 3 4
4 2 6 4 9 2 7 2 1 2 6 1 4 3 3 8 8 1 3 2 5 3 4 6
5 1 6 3 4 1 6 2 2 1 5 2 4 3 3 8 8 1 3 2 1 4 1 2
在Python中,什么是最快的方法pandas.DataFrame?谢谢!在
您也可以使用} 。在
sklearn
库中的^{使用
pd.factorize
用于分解列
^{pr2}$用于分解行
将整个数据帧分解在一起
以下是两个不同的解决方案的总结,基于前面的答案,它们在我的案例中的表现方式。在
相关问题 更多 >
编程相关推荐