如何将Pandas Dataframe中的字符串转换为列表或字符数组? [英] How to convert strings in a Pandas Dataframe to a list or an array of characters?

查看:205
本文介绍了如何将Pandas Dataframe中的字符串转换为列表或字符数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 data 的数据框,其中的一列包含字符串.我想从字符串中提取字符,因为我的目标是对它们进行一次热编码并使其可用于分类.包含字符串的列存储在预测变量中,如下所示:

I have a dataframe called data, a column of which contains strings. I want to extract the characters from the strings because my goal is to one-hot encode them and make the usable for classification. The column containing the strings is stored in predictors as follows:

predictors = pd.DataFrame(data, columns = ['Sequence']).to_numpy()

打印后的结果是:

[['DKWL']
 ['FCHN']
 ['KDQP']
 ...
 ['SGHC']
 ['KIGT']
 ['PGPT']]

,而我的目标是获得类似的东西:

,while my goal is to get somehing like:

[['D', 'K', 'W', 'L']
 ...
 ['P', 'G', 'P, 'T']]

根据我的理解,这是一种更适合单次编码的形式.

which from my understanding is a more appropriate form for one-hot encoding.

我已经尝试过在此处提供的答案如何将字符串字符转换为列表?或在此处

I have already tried answers provided here How do I convert string characters into a list? or here How to create a list with the characters of a string? to no success.

具体来说,我也尝试过:

Specifically, I also tried this:

for row in predictors:
    row = list(row)

但结果的格式与预测变量相同,即

but the result is in the same form as predictors, i.e.

 [['DKWL']
 ['FCHN']
 ['KDQP']
 ...
 ['SGHC']
 ['KIGT']
 ['PGPT']]

推荐答案

您可以使用 list 通过列表理解将值转换为字母,然后根据需要转换为 array :

You can convert values to letters by list comprehension with list and then to array if necessary:

predictors = np.array([list(x) for x in data])

或转换列 predictors ['Sequence'] :

a = np.array([list(x) for x in predictors['Sequence']])
print(a)
[['D' 'K' 'W' 'L']
 ['F' 'C' 'H' 'N']
 ['K' 'D' 'Q' 'P']
 ['S' 'G' 'H' 'C']
 ['K' 'I' 'G' 'T']
 ['P' 'G' 'P' 'T']]

对于系列使用:

s = predictors['Sequence'].apply(list)
print(s)
0    [D, K, W, L]
1    [F, C, H, N]
2    [K, D, Q, P]
3    [S, G, H, C]
4    [K, I, G, T]
5    [P, G, P, T]
Name: Sequence, dtype: object

这篇关于如何将Pandas Dataframe中的字符串转换为列表或字符数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆